django-queryset

Creating custom Field Lookups in Django

二次信任 提交于 2020-01-10 10:38:32
问题 How do you create custom field lookups in Django? When filtering querysets, django provides a set of lookups that you can use: __contains , __iexact , __in , and so forth. I want to be able to provide a new lookup for my manager, so for instance, someone could say: twentysomethings = Person.objects.filter(age__within5=25) and get back all the Person objects with an age between 20 and 30. Do I need to subclass the QuerySet or Manager class to do this? How would it be implemented? 回答1: As of

Django: Filter for get_foo_display in a Queryset

自闭症网瘾萝莉.ら 提交于 2020-01-09 19:30:08
问题 I've been trying to filter a queryset on a simple model but with no luck so far. Here is my model: class Country(models.Model): COUNTRY_CHOICES = ( ('FR', _(u'France')), ('VE', _(u'Venezuela')), ) code = models.CharField(max_length=2, choices=COUNTRY_CHOICES) def __unicode__(self): return self.get_code_display() And I would like to do something like: Country.objects.filter(get_code_display__icontains="france") Country.objects.filter(code__display__icontains="france") Country.objects.filter

Django: Filter for get_foo_display in a Queryset

北战南征 提交于 2020-01-09 19:29:41
问题 I've been trying to filter a queryset on a simple model but with no luck so far. Here is my model: class Country(models.Model): COUNTRY_CHOICES = ( ('FR', _(u'France')), ('VE', _(u'Venezuela')), ) code = models.CharField(max_length=2, choices=COUNTRY_CHOICES) def __unicode__(self): return self.get_code_display() And I would like to do something like: Country.objects.filter(get_code_display__icontains="france") Country.objects.filter(code__display__icontains="france") Country.objects.filter

What is a Django QuerySet?

℡╲_俬逩灬. 提交于 2020-01-09 16:06:27
问题 When I do this, >>> b = Blog.objects.all() >>> b I get this: >>>[<Blog: Blog Title>,<Blog: Blog Tile>] When I query what type b is, >>> type(b) I get this: >>> <class 'django.db.models.query.QuerySet'> What does this mean? Is it a data type like dict , list , etc? An example of how I can build data structure like a QuerySet will be appreciated. I would want to know how Django builds that QuerySet (the gory details). 回答1: Yes, it's just another type, built like every other type. 回答2: A django

Django lazy QuerySet and pagination

◇◆丶佛笑我妖孽 提交于 2020-01-09 04:22:08
问题 I read here that Django querysets are lazy, it won't be evaluated until it is actually printed. I have made a simple pagination using the django's built-in pagination. I didn't realize there were apps already such as "django-pagination", and "django-endless" which does that job for. Anyway I wonder whether the QuerySet is still lazy when I for example do this entries = Entry.objects.filter(...) paginator = Paginator(entries, 10) output = paginator.page(page) return HttpResponse(output) And

How to make filter query in Django?

我只是一个虾纸丫 提交于 2020-01-06 15:33:09
问题 I have the following model.py . I want to create a filter during Input. SubCategory automatically extracted from members Category. While the input current during subcategory will display the entire contents without filter. I want to choose one parent, then just out of the parent's members only. > from django.db import models > > > class Category(models.Model): > > name = models.CharField(max_length=100) > > > > def __unicode__(self): > > return self.name > > > > class SubCategory(models.Model

Django QuerySet First in Group

ε祈祈猫儿з 提交于 2020-01-06 12:43:30
问题 I have a queryset that looks like: {id, group_id, date} For each group, keyed by the group_id , I'd like to return the id with the earliest date . How would I do that in a django query? Or is it more efficient to order_by date and then use a python dict to ignore all elements with the same group_id after the first? 回答1: Django doesn't really have the GROUP BY operator. You could try to mimic it with ordering and distinct() . Try something like this: queryset.order_by('group_id', 'date')

Why does my form in Django save successfully except for one field?

雨燕双飞 提交于 2020-01-06 07:25:34
问题 my form in forms.py is then passed to this method in my views.py, if I go into python shell and print objects from MyProfile, all of the fields show values except for nearbyzips, which shows None. As you can see below, I am trying to manually assign a value to nearbyzips when the form is saved. inside views.py @secure_required @login_required def profile_edit(request, username, edit_profile_form=EditProfileForm, template_name='userena/profile_form.html', success_url=None, extra_context=None,

Merge lists while showing zeros in python

让人想犯罪 __ 提交于 2020-01-06 07:22:20
问题 I created a Django QuerySet which count values but unfortunately it do not show 0 values. Therefore I want to merge my two list like left join in SQL. I show my inputs and desired outputs . INPUT 1 (Django Query 1): I print it out like this: for i in query_1: print (i['day'], i['count_1']) 2018-01-17 00:00:00+01:00 49 2018-01-16 00:00:00+01:00 139 2018-01-15 00:00:00+01:00 144 2018-01-14 00:00:00+01:00 142 2018-01-13 00:00:00+01:00 141 2018-01-12 00:00:00+01:00 144 2018-01-11 00:00:00+01:00

Merge lists while showing zeros in python

青春壹個敷衍的年華 提交于 2020-01-06 07:22:05
问题 I created a Django QuerySet which count values but unfortunately it do not show 0 values. Therefore I want to merge my two list like left join in SQL. I show my inputs and desired outputs . INPUT 1 (Django Query 1): I print it out like this: for i in query_1: print (i['day'], i['count_1']) 2018-01-17 00:00:00+01:00 49 2018-01-16 00:00:00+01:00 139 2018-01-15 00:00:00+01:00 144 2018-01-14 00:00:00+01:00 142 2018-01-13 00:00:00+01:00 141 2018-01-12 00:00:00+01:00 144 2018-01-11 00:00:00+01:00