I have a simple Relation model, where a user can follow a tag just like stackoverflow.
class Relation(models.Model):
user = AutoOneToOneField(User)
follows_tag = models.ManyToManyField(Tag, blank=True, null=True, through='TagRelation')
class TagRelation(models.Model):
user = models.ForeignKey(Relation, on_delete=models.CASCADE)
following_tag = models.ForeignKey(Tag, on_delete=models.CASCADE)
pub_date = models.DateTimeField(default=timezone.now)
class Meta:
unique_together = ['user', 'following_tag']
Now, to get the results of all the tags a user is following:
kakar = CustomUser.objects.get(email="kakar#gmail.com")
tags_following = kakar.relation.follows_tag.all()
This is fine.
But, to access intermediate fields I have to go through a big list of other queries. Suppose I want to display when the user started following a tag, I will have to do something like this:
kakar = CustomUser.objects.get(email="kakar#gmail.com")
kakar_relation = Relation.objects.get(user=kakar)
t1 = kakar.relation.follows_tag.all()[0]
kakar_t1_relation = TagRelation.objects.get(user=kakar_relation, following_tag=t1)
kakar_t1_relation.pub_date
As you can see, just to get the date I have to go through so much query. Is this the only way to get intermediate values, or this can be optimized? Also, I am not sure if this model design is the way to go, so if you have any recomendation or advice I would be very grateful. Thank you.
You need to use Double underscore i.e. ( __ ) for ForeignKey lookup,
Like this :
user_tags = TagRelation.objects.filter(user__user__email="kakar#gmail.com").values("following_tag__name", "pub_date")
If you need the name of the tag, you can use following_tag__name in the query and if you need id you can use following_tag__id.
And for that you need to iterate through the result of above query set, like this:
for items in user_tags:
print items['following_tag__name']
print items['pub_date']
One more thing,The key word values will return a list of dictionaries and you can iterate it through above method and if you are using values_list in the place of values, it will return a list of tuples. Read further from here .
Related
I'm trying to get all values in current table, and also get some fields in related tables.
class school(models.Model):
school_name = models.CharField(max_length=256)
school_type = models.CharField(max_length=128)
school_address = models.CharField(max_length=256)
class hometown(models.Model):
hometown_name = models.CharField(max_length=32)
class person(models.Model):
person_name = models.CharField(max_length=128)
person_id = models.CharField(max_length=128)
person_school = models.ForeignKey(school, on_delete=models.CASCADE)
person_ht = models.ForeignKey(hometown, on_delete=models.CASCADE)
how to quick select all info i needed into a dict for rendering.
there will be many records in person, i got school_id input, and want to get all person in this school, and also want these person's hometown_name shown.
i tried like this, can get the info i wanted. And any other quick way to do it?
m=person.objects.filter(person_school_id=1)
.values('id', 'person_name', 'person_id',
school_name=F('person_school__school_name'),
school_address=F('person_school__school_address'),
hometown_name=F('person_ht__hometown_name'))
person_name, person_id, school_name, school_address, hometown_name
if the person have many fields, it will be a hard work for list all values.
what i mean, is there any queryset can join related tables' fields together, which no need to list fields in values.
Maybe like this:
m=person.objects.filter(person_school_id=1).XXXX.values()
it can show all values in school, and all values in hometown together with person's values in m, and i can
for x in m:
print(x.school_name, x.hometown_name, x.person_name)
You add a prefetch_related query on top of your queryset.
prefetch_data = Prefetch('person_set, hometown_set, school_set', queryset=m)
Where prefetch_data will prepare your DB to fetch related tables and m is your original filtered query (so add this below your Person.objects.filter(... )
Then you do the actual query to the DB:
query = query.prefetch_related(prefetch_data)
Where query will be the actual resulting query with a list of Person objects (so add that line below the prefetch_data one).
Example:
m=person.objects.filter(person_school_id=1)
.values('id', 'person_name', 'person_id',
school_name=F('person_school__school_name'),
school_address=F('person_school__school_address'),
hometown_name=F('person_ht__hometown_name'))
prefetch_data = Prefetch('person_set, hometown_set, school_set', queryset=m)
query = query.prefetch_related(prefetch_data)
In that example I've broken down the queries into more manageable pieces, but you can do the whole thing in one big line too (less manageable to read though):
m=person.objects.filter(person_school_id=1)
.values('id', 'person_name', 'person_id',
school_name=F('person_school__school_name'),
school_address=F('person_school__school_address'),
hometown_name=F('person_ht__hometown_name')).prefetch_related('person, hometown, school')
I have 2 models which i wanna join
class CollectionBook(models.Model):
collection = models.ForeignKey('Collection')
book = models.ForeignKey('Book')
class Meta:
unique_together = (('collection', 'book'))
class Book(models.Model):
main_author = models.ForeignKey('Author', verbose_name=u'Main author')
publisher = models.ForeignKey('Publisher', verbose_name=u'Publisher')
title = models.CharField(unique=False, max_length=255, verbose_name=u'Title')
text = models.TextField(verbose_name=u'Book text', max_length=523000)
I tried to do it this way
book = Book.objects.extra(
select = {'id':'mainapp_collectionbook.book_id'})
book.query.join((None,'mainapp_collectionbook',None,None))
connection = (
Book._meta.db_table,
CollectionBook.book.field,
)
book.query.join(connection, promote=True)
But it didn't work out and gave me error
Could you offer me another pythonic solutions of this problem or improve my way of doing it, I don't wanna write sql query, I hope that there are better django orm functions for this
Taking the clarification from the comment:
I have another table "Collection". I want select books where collection to which book belongs is in allowed collection list. I generate collection list before this query
First, replace your explicit CollectionBook table with a ManyToManyField on either Book or Collection. For this example I'll assume it's on Book, since that keeps the syntax clearer and the Collection model isn't shown.
class Book(models.Model):
main_author = models.ForeignKey('Author', verbose_name=u'Main author')
publisher = models.ForeignKey('Publisher', verbose_name=u'Publisher')
title = models.CharField(unique=False, max_length=255, verbose_name=u'Title')
text = models.TextField(verbose_name=u'Book text', max_length=523000)
collection = models.ManyToManyField('Collection')
Then you can use __ syntax to follow relationships:
Books.objects.filter(collection__in=SOME_LIST_OF_COLLECTIONS).distinct()
If you need additional information on the book/collection relation, EG a date collected or something, specify a through argument to the ManyToManyField.
if I understand correctly, you can try it, but don't forget to change the YOU_CONDITION
allow_collections = CollectionBook.objects.filter(YOU_CONDITION)
books_pks = allow_collections.values_list('book__pk', flat=true)
Book.objects.filter(pk__in=books_pks)
I have a History model like below
class History(models.Model):
class Meta:
app_label = 'subscription'
ordering = ['-start_datetime']
subscription = models.ForeignKey(Subscription, related_name='history')
FREE = 'free'
Premium = 'premium'
SUBSCRIPTION_TYPE_CHOICES = ((FREE, 'Free'), (Premium, 'Premium'),)
name = models.CharField(max_length=32, choices=SUBSCRIPTION_TYPE_CHOICES, default=FREE)
start_datetime = models.DateTimeField(db_index=True)
end_datetime = models.DateTimeField(db_index=True, blank=True, null=True)
cancelled_datetime = models.DateTimeField(blank=True, null=True)
Now i have a queryset filtering like below
users = get_user_model().objects.all()
queryset = users.exclude(subscription__history__end_datetime__lt=timezone.now())
The issue is that in the exclude above it is checking end_datetime for all the rows for a particular history object. But i only want to compare it with first row of history object.
Below is how a particular history object looks like. So i want to write a queryset filter which can do datetime comparison on first row only.
You could use a Model Manager method for this. The documentation isn't all that descriptive, but you could do something along the lines of:
class SubscriptionManager(models.Manager):
def my_filter(self):
# You'd want to make this a smaller query most likely
subscriptions = Subscription.objects.all()
results = []
for subscription in subscriptions:
sub_history = subscription.history_set.first()
if sub_history.end_datetime > timezone.now:
results.append(subscription)
return results
class History(models.Model):
subscription = models.ForeignKey(Subscription)
end_datetime = models.DateTimeField(db_index=True, blank=True, null=True)
objects = SubscriptionManager()
Then: queryset = Subscription.objects().my_filter()
Not a copy-pastable answer, but shows the use of Managers. Given the specificity of what you're looking for, I don't think there's a way to get it just via the plain filter() and exclude().
Without knowing what your end goal here is, it's hard to say whether this is feasible, but have you considered adding a property to the subscription model that indicates whatever you're looking for? For example, if you're trying to get everyone who has a subscription that's ending:
class Subscription(models.Model):
#property
def ending(self):
if self.end_datetime > timezone.now:
return True
else:
return False
Then in your code: queryset = users.filter(subscription_ending=True)
I have tried django's all king of expressions(aggregate, query, conditional) but was unable to solve the problem so i went with RawSQL and it solved the problem.
I have used the below SQL to select the first row and then compare the end_datetime
SELECT (end_datetime > %s OR end_datetime IS NULL) AS result
FROM subscription_history
ORDER BY start_datetime DESC
LIMIT 1;
I will select my answer as accepted if not found a solution with queryset filter chaining in next 2 days.
I apologize in advance if my question has already been there, but I have not found.
there is a model:
class Artikul_cabinets(models.Model):
artikul_cabinets = models.CharField(verbose_name="Артикул шкафа", max_length=20)
title_cabinets = models.CharField(verbose_name="Описание шкафа", max_length=200)
width_cabinets = models.ManyToManyField(Width_cabinets)
depth_cabinets = models.ManyToManyField(Depth_cabinets)
unit_cabinets = models.ManyToManyField(Unit_cabinets)
weight_cabinets = models.ManyToManyField(Weight_cabinets)
type_cabinets = models.ForeignKey(Type_cabinets, default=1)
color_cabinets = models.ForeignKey(Color_cabinets)
glass_cabinets = models.ManyToManyField(Glass_cabinets)
class Meta:
verbose_name_plural = "Артикул шкафа"
def __str__(self):
return self.artikul_cabinets
It is necessary to make the selection on the field
glass_cabinets = models.ManyToManyField(Glass_cabinets)
The selection is done as follows
data = Artikul_cabinets.objects.filter(Q(glass_cabinets=perf) &
Q(glass_cabinets=glass)
perf and glass the variables with different values.
And I returned to my empty QuerySet, although the database element with the parameters 'perf' and 'glass' are present in the record.
Tell me what I'm doing wrong.
also tried:
data = Artikul_cabinets.objects.filter(Q(glass_cabinets=perf),
Q(glass_cabinets=glass)
and also did not work, though if you put the operator '|' the conditions or work out correctly.
So I think you should do Artikul_cabinets.objects.filter(glass_cabinets=perf).filter(glass_cabinets=glass)
check How to filter model results for multiple values for a many to many field in django
The values_list in filtering object, really helps me a lot in providing solution within django view.
My code is like the following and this one works:
values_list_ac = realdata.objects.filter(product = '1').values_list('company', 'brand', 'created_by__username')
while username is the field exists in different model outside the current realdata model.
But the following code doesn't work, for I want to show the value of ac_type, which based on choices field within the same realdata model. (I try to solve it by using the same solution which work in template):
values_list_ac = realdata.objects.filter(product = '1').values_list('company', 'brand', 'created_by__username', 'get_ac_type_display')
Is there a solution other than get_ac_type_display to show the field value?
I really appreciate for some shed of light.
Edit:
This my model:
class realdata(models.Model):
company = models.CharField(max_length=60, verbose_name="Company")
brand = models.CharField(_('brand'), max_length=60)
model = models.CharField(max_length=60)
type_choices = (
(u'1', u'Inverter'),
(u'2', u'Non-Inverter'),
)
ac_type = models.CharField(max_length=60, verbose_name="Type", choices=type_choices)
created_by = models.ForeignKey(User)
Many Thanks!
The values_list function will just get the values stored in the database. When defining choices on your model's field, it will store the first value of the tuple, hence this will be what you'll retrieve.
This means that you have to look at the choices tuple to determine the display value for the item. The purpose of the get_foo_display is to give you this human-readable value, but it needs a model instance to work on.
So, a solution to resolving this yourself would be to inspect the choices, and convert the data accordingly. The following should be able to do this:
result = []
for p in realdata.objects.filter(product='1').values_list(
'company', 'brand', 'created_by__username', 'ac_type'):
choice = {k: v for k, v in realdata.type_choices}[p[-1]]
result.append(list(p[:-1]) + [choice])
The result variable will contain the converted list. The new variable is needed because the values_list function will return a list of tuples; the latter being unmutable. Also, take care to have the value you'll want to resolve as the last item in your values_list call, or adapt the above to match.