python


Extract OneToOne Field in django model


class Post(models.Model):
created_time = models.DateTimeField()
comment_count = models.IntegerField(default=0)
like_count = models.IntegerField(default=0)
group = models.ForeignKey(Group)
class MonthPost(models.Model):
created_time = models.DateTimeField()
comment_count = models.IntegerField(default=0)
like_count = models.IntegerField(default=0)
group = models.ForeignKey(Group)
post = models.OneToOneField(Post)
I use this two models. MonthPost is part of Post.
I want to use MonthPost when filtered date is smaller than month.
_models = Model.extra(
select={'score': 'like_count + comment_count'},
order_by=('-score',)
)
I use extra about above two models. Post works well, but MonthPost doesn't work.
django.db.utils.ProgrammingError: column reference "like_count" is ambiguous
LINE 1: ... ("archive_post"."is_show" = false)) ORDER BY (like_count...
This is the error message.
_models.values_list("post", flat=True)
And then, I want to extract OneToOne field(post) from MonthPost.
I try to use values_list("post", flat=True). It return only id list.
I need to post object list for django rest framework.
I don't' quite understand what you are trying to achieve with your MonthPost model and why it duplicates Post fields. With that being said I think you can get the results you want with this info.
First of all extra is depreciated see the docs on extra. In either case, your select is not valid SQL syntax, your query should look more like this:
annotate(val=RawSQL(
"select col from sometable where othercol =%s",
(someparam,)))
However, what you are after here requires neither extra or RawSql. These methods should only be used when there is no built in way to achieve the desired results. When using RawSql or extra, you must tailor the SQL for your specific backed. Django has built in methods for such queries:
qs = Post.objects.all().annotate(
score=(Count('like_count') + Count('comment_count'))
A values_list() query needs to explicitly list all fields from related models and extra or annotated fields. For MonthPost it should look like this:
MonthPost.objects.all().values_list('post', 'post__score', 'post__created_time')
Finally, if the purpose of MonthPost is simply to list the posts with he greatest score for a given month, you can eliminate the MonthPost model entirely and query your Post model for this.
import datetime
today = datetime.date.today()
# Filter for posts this month
# Annotate the score
# Order the results by the score field
qs = Post.objects\
.filter(created_time__year=today.year, created_time__month=today.month)\
.annotate(score=(Count('like_count') + Count('comment_count'))\
.order_by('score')
# Slice the top ten posts for the month
qs = qs[:10]
The code above is not tested, but should give you a better handle on how to perform these types of queries.

Related Links

Ndb entry .put() not executed?
Tensorflow: NaNs propagating throughout network, even though using sparse_softmax etc
How to do a Python argparse mutually required argument group
scaling websocket game application server
Is there a way to find a character's Unicode code point in Python 2.7?
figtext datetime function matplotlib
environment variable in ubuntu
Cross-platform Python Executables
Extract subarray from collection of 2D coordinates?
tkinter populate treeview using threading pool
How to make a function determining the winner of Tic-Tac-Toe more concise
Django update model entry using form fails
ctypes using HRESULT(python)
How to export property values with django-import-export
Plotting Coordinate Lines Using Matplotlib
BASH - Summarising information present in 2 genotype data columns in one column (ped file) [closed]

Categories

HOME
paypal
json
vue.js
symfony
plsql
websphere
vb6
xamarin.android
sed
ncurses
vs-team-services
agile
hyperledger-fabric
android-viewpager
public-key-encryption
symfony2-easyadmin
ssms-2016
zope
atlassian-plugin-sdk
avplayeritem
java-ee-7
image-quality
upnp
classpath
ioc-container
protobuf-net
node-horseman
nunit-3.0
ksoap
es-shell
mongoexport
s3cmd
alchemy.js
web-development-server
dojox.mobile
taglib
onmouseover
dimple.js
decompress
pluck
teiid
date-range
sqlexception
project-template
payu
libtiff.net
system.web.ui.webcontrols
tomee
mailcatcher
spark-cassandra-connector
metalsmith
com-interop
multilingual-app-toolkit
twgl.js
axes
anythingslider
anti-patterns
paypal-nvp
type-mismatch
divide-by-zero
pgm
isml
google-earth-plugin
client-side-validation
django-1.6
kallithea
moai
gyroscope-framework
livechat
hidden-field
em
iostream
sthttprequest
method-overriding
valence
httpcontext
unison
openkinect
mimosa
jquery-tabs
ng-hide
threadx
ng-pattern
google-closure-library
iconv
fraud-prevention
fortran77
zend-lucene
vt100
sequelpro
socketstream
mpmovieplayer
redirectstandardoutput
expression-evaluation
jpf
handwriting
phpcrawl
drawtext
system-codedom-compiler

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App