I am sorry if this is a silly question, because after reading some documentation I was able to render every model field in my view, but I am stuck with a list of DateField label...
So in my model I have a :
date_realization = models.DateField(_('Realised in'),
default=timezone.now)
Field, but I don't know why I can't select it in my views.py.
Well, I mean I have articles and each article has a date_realization. I want to get the list of those date and render it into the context method but I don't know how to do it.
Does anyone has a clue about it ?
If you want to get a list of date_realization for all articles you could do:
date_realization_for_articles = Article.objects.values_list('date_realization',
flat=True).distinct()
django doc for values_list.
Related
I need to take one field from the form, make calculations and insert the result into the second field. If you use a function, you can use cleaned _data.get ('first_field') for this, but how to do this using UpdateView?
Thank you!
If you want t odo it from the backend then preferring to do it as:
record = form.save(commit=False)
record.field1 = record.field2
record.save()
Otherwise, You need to do it from jQuery or JS before submitting the form.
In django by default the form for a model foreign key is a ModelChoiceField (where you can select out of a list of all possible models). And it's possible to change these with the query set attribute, like
// in forms.py
self.fields['possible_cars'].queryset = somequeryset
But I'm in a situation where I have a list of stuff, not a queryset, and since there is no way to convert a list into a queryset, i'm not sure how to make the options for my ModelChoiceField similar to the list of models I want. (Since they take a queryset by default, i'm assuming they get a list from that query anyways, so this kinda thing should be possible).
I tried self.fields['possible_cars']._choices = mylist , but it won't work.
Any ideas guys?
Assuming your field take a Car queryset, you can construct one like the following:
mylist = ['BMW', 'Lamborghini', 'Porsche']
cars = Car.objects.filter(name__in=mylist)
self.fields['possible_cars'].queryset = cars
Is it possible to retrieve the subset of fields using Django mongodb nonrel. I am totally new to python, but have good knowledge in mongo.
My requirement is very straight forward, I wanted to query the collection by its embedded field and return only some specific fields
I could do that in mongodb by
db.Contract.find({'owner.name':'Ram'},{'address':1})
and I tried this in django
Contract.objects.filter(owner__name='Ram')
but it throws an error
raise FieldError("Join on field %r not permitted. Did you misspell %r
for the lookup type?" % (name, names[pos + 1])) FieldError: Join on
field 'owner' not permitted. Did you misspell 'name' for the
lookup type?
am totally struck here. I believe i have my models as specified in the documentation.
class SimplePerson(models.Model):
name = models.CharField(max_length=255)
user_key = models.CharField(max_length=255)
class Contract(models.Model):
owner = EmbeddedModelField('SimplePerson')
title = models.CharField(max_length=120, )
This is really weird. I could't find any reference in the documentation site about how to query the embedded field & retrieve the subset of fields.
Finally I used raw_query to query the embedded field
Contract.objects.raw_query({'owner.name':'Ram'})
But still not able to figure out how to retrieve the subset of fields. Can someone help me out?
Subobject filters aren't possible yet so you need to drop down to raw_query (which you already figured out). To retrive a subset of fields, use .values('field1', 'field2', ...).
Filtering by EmbeddedField is now possible with the following syntax:
Contract.objects.filter(owner={'name': 'Ram'})
I'm using tastypie to create json from my django models however I'm running into a problem that I think should have a simple fix.
I have an object Blogs wich has Comment object children. I want to be able to do something like this with my json:
/api/v1/blogs/?order_by=comment_count
But I can't figure out how to sort on a field that's not part of the original comment/ blog model. I create comment_count myself in a dehydrate method that just takes the array of comments and returns comments.count()
Any help would be much appreciated - I can't seem to find any explanation.
If I understood correctly this should help:
Blog.objects.annotate(comment_count=Count('comments')).order_by('comment_count')
You might be able to do it with extra like something like:
Blog.objects.extra(
select={
'entry_count': 'SELECT COUNT(*) FROM blog_entry WHERE blog_entry.blog_id = blog_blog.id'
},
order_by = ['-entry_count'],
)
I haven't tested this, but it should work. The caveat is it will only work with a relational database.
I'd like to have several fields in my form being rendered as ChoiceFields which get their content from the database.
I was thinking something like:
class SeriesForm(ModelForm):
series = forms.ChoiceField(choices=Series.objects.all())
class Meta:
model = Series
exclude = ('model', 'date_added',)
But the field series is now not appearing at all in my form. What am I missing?
After trying the solution (using the ModelChoiceField), I'm still seeing the same issue. Here is my code:
series = forms.ModelChoiceField(queryset=Series.objects.values('series'),
empty_label=" ")
Use a ModelChoiceField instead.