django-queryset

django query based on dynamic property()

大憨熊 提交于 2019-12-04 00:26:35
问题 I was wondering if there was a way to use Django's filter() on query sets using a dynamically generated python property using property() . I have first_name and last_name of every user, and I want to filter based on their concatenated name first_name last_name . (The reason behind this is that when I do autocomplete I search to see if the query matches first name, last name, or part of the concatenation. I want John S to match John Smith , for example. I created a property of name : def _get

Django - Unique list from QuerySet

好久不见. 提交于 2019-12-04 00:04:57
I have a filtered QuerySet which has a ManyToMany field 'Client'. I want to create a unique dict of all the Client objects in the query set so: Projects Queryset: - Project1.client = <Client: 1> - Project2.client = <Client: 1> - Project3.client = <Client: 2> - Project4.client = <Client: 2> - Project5.client = <Client: 3> class Project(models.Model): client = models.ForeignKey(Client, blank=True, null=True) I want to end up with a dict of client objects: {<Client: 1>,<Client: 2>,<Client: 3>} Some help would be appreciated :) Yuval Adam Project.objects.values('client').distinct() Link to Django

Selecting specific fields using select_related in Django

烂漫一生 提交于 2019-12-04 00:02:55
I have two models Article and Blog related using a foreign key. I want to select only blog name while extracting the article. articles = Articles.objects.all().select_related('blog__name') The query generated shows that it selected all the fields from the Blog model. I tried using only() and defer() with select_related but both didn't work out. articles = Articles.objects.all().select_related('blog__name').only('blog__name', 'title', 'create_time') The above query resulted in error: Invalid field name(s) given in select_related: Choices are: blog How do i generate a query so that only article

Django Boolean Queryset Filter Not Working

不羁的心 提交于 2019-12-03 23:47:11
问题 This has been frustrating me for the better part of an hour. I have the following model: sold= models.BooleanField(default=False) And the following view code: properties = Property.objects.filter(sold=False).order_by('-created_on'); And the following values in my sqlite3 database: sqlite> select sold from clients_property; 1 1 1 1 1 And the following template code DOES work (as in, hides the sold items): {% if not property.sold %} Anyone know why the query set filter isn't working or why I'm

simple way for QuerySet union and subtraction in django?

♀尐吖头ヾ 提交于 2019-12-03 22:40:30
Consider two QuerySet objects of the same class. Is there a simple way to unify them into a single QuerySet by calculating the union? Also, is there a simple way to subtract them? Removing all elements that appear in both sets from one of the sets? Going back to django's documentation , you can: new_query_set = query_set_1 | query_set_2 This works as a logical OR which is actually addition without duplicates. This answers the addition aspect and AFAIK does not hit the db at all ! new_query_set = query_set_1 & query_set_2 This works as a logical AND. Still missing how to subtract QuerySets. It

How to modify a queryset and save it as new objects?

拟墨画扇 提交于 2019-12-03 20:11:07
I need to query for a set of objects for a particular Model, change a single attribute/column ("account"), and then save the entire queryset's objects as new objects/rows. In other words, I want to duplicate the objects, with a single attribute ("account") changed on the duplicates. I'm basically creating a new account and then going through each model and copying a previous account's objects to the new account, so I'll be doing this repeatedly, with different models, probably using django shell. How should I approach this? Can it be done at the queryset level or do I need to loop through all

Django Datetime Field Query - Order by Time/Hour

元气小坏坏 提交于 2019-12-03 18:15:58
问题 I am performing a query to return all WorkOrder s that have an appointment scheduled for today using the foreign key Appointment 's datetime field start . This is the query I am using and it is working exactly as expected. WorkOrder.objects.filter(appointment__start__year=date.today().year, appointment__start__month=date.today().month, appointment__start__day=date.today().day) I would like to sort them by date descending and hour ascending. The order I want would look something like this: 11

Django prefetch_related with m2m through relationship

耗尽温柔 提交于 2019-12-03 15:21:53
I have the following models class Film(models.Model): crew = models.ManyToManyField('Person', through='Role', blank=True) class Role(models.Model): person = models.ForeignKey('Person') film = models.ForeignKey('Film') person_role = models.ForeignKey(RoleType) credit = models.CharField(max_length=200) credited_as = models.CharField(max_length=100) class RoleType(models.Model): """Actor, director, makeup artist...""" name = models.CharField(max_length=50) class Person(models.Model): slug = models.SlugField(max_length=30, unique=True, null=True) full_name = models.CharField(max_length=255) A Film

Breaking data into multiple display columns with Django

我怕爱的太早我们不能终老 提交于 2019-12-03 13:54:59
问题 Overlap in terminology makes search for answers difficult for this one. I'm looking for advice on the best way to implement a multiple-column display of my QuerySet that fills each column top to bottom over X columns. Meaning that the number of items in each column equals the QuerySet count divided by X (number of columns). Using Offset doesn't work for this because I would like my data to grow into 4 columns without updating the offset manually. CSS floats work visually, but leave the data

Subclassed django models with integrated querysets

二次信任 提交于 2019-12-03 13:13:40
Like in this question , except I want to be able to have querysets that return a mixed body of objects: >>> Product.objects.all() [<SimpleProduct: ...>, <OtherProduct: ...>, <BlueProduct: ...>, ...] I figured out that I can't just set Product.Meta.abstract to true or otherwise just OR together querysets of differing objects. Fine, but these are all subclasses of a common class, so if I leave their superclass as non-abstract I should be happy, so long as I can get its manager to return objects of the proper class. The query code in django does its thing, and just makes calls to Product().