I have query like this in Django:
shared_file = File.objects.filter(id__in= Share.objects.filter(users_id = log_id).values_list('files', flat=True)).annotate(count=Count('share__shared_user_id'))
file1 = [i.file_name for i in shared_file]
shared_username = [User.objects.filter(id__in= Share.objects.filter(users_id = log_id, files__file_name=k).values_list('shared_user_id', flat=True)).values_list('username') for k in file1]
I want to join them so that I can loop over it and find the usernames with whom the file is shared.
You can use select_related() and fetch related objects in one query (and avoid list comprehensions with queries):
I assumed that use model definiton as in your previous question:
class Share(models.Model):
users = models.ForeignKey(User)
files = models.ForeignKey(File)
shared_user_id = models.IntegerField()
shared_date = models.TextField()
You can fetch Shares with User and File in one shot using:
shares = Share.objects.select_related('users', 'files').filter(users_id = log_id).all()
for share in shares:
share # Share model
share.users # User model
share.files # File model
Use itertools chain from the STL http://docs.python.org/2/library/itertools.html#itertools.chain
chain(file1, shared_username)
Related
I have three models:
class Video(models.Model):
video_pk = models.AutoField(primary_key=True)
author_fk = models.ForeignKey(GRUser, related_name='uploaded_videos', db_column='author_fk')
class VideoLike(models.Model):
video_like_pk = models.AutoField(primary_key=True)
video_fk = models.ForeignKey(Video, related_name='likes_list', db_column='video_fk')
author_fk = models.ForeignKey(GRUser, related_name='video_likes_list', db_column='author_fk')
video_like_dttm = models.DateTimeField(auto_now_add=True)
class VideoStats(models.Model):
video_fk_pk = models.OneToOneField(Video, primary_key=True, db_column='video_fk_pk', related_name='stats')
likes_num = models.BigIntegerField(default=0)
To prevent the same database hits I periodically cache some popular videos:
from django.core.cache import cache
qs_all = models.Video.objects.select_related('author_fk', 'stats').filter(publication_status=models.Video.PUBLISHED).order_by('-stats__likes_num')
cache.set('popular_videos_all', qs_all[:length])
When returning those videos through my API (Django-Rest-Framework) I should add video_like_pk of current user's like if the user is authenticated and the like exists.
I can do it using prefetch_related but it makes two calls to the database and doesn't use the cached results. I want to fetch from the database only VideoLikes of current user and take the rest from the cache. Is it possible? Maybe there is a completely other approach which is better?
I have the following query:
prefetch = Prefetch('books', queryset=Book.objects.filter(is_published=True),
to_attr='published_books')
profiles = Profile.objects.prefetch_related(prefetch)
This selects all profiles and populates them with published books.
However, I want only profiles, that actually have published books (in other words len(profile.published_books) > 0).
How can I achieve it in orm?
UPDATE:
class Book(Model):
profile = ForeignKey(Profile, related_name="books", related_query_name="book")
name = CharField(max_length=250)
is_published = BooleanField(default=True)
class Meta:
unique_together = (('profile', 'name'),)
profile_ids = Profile.objects.filter(book__is_published=True).values_list("pk", flat=True).distinct()
profiles = Profile.objects.filter(pk__in=profile_ids).prefetch_related(prefetch)
this will result a subquery that locate correct profile ids and then return any profile that match one of these ids.
use of distinct function has no effect on result but I am guessing it could improve performance (not really sure)
I have the following models:
def Order(models.Model):
receiver = models.ForeignKey(Receiver)
warehouse = models.ForeignKey(Warehouse)
def Receiver(models.Model):
user = models.ForeignKey(User) #this is not made one to one because user can have more than one receiver
name = ...
zipcode = ...
def Warehouse(models.Model):
city = ...
street = ...
I want to select all Warehouse entries related to request.User object. The only way i can do this now is:
orders = Order.objects.filter(receiver__user=request.User)
# here i set orders warehouse ids to list called ids
user_warehouses = Warehouse.objects.filter(pk__in=ids)
But i have a strong feeling that i am inventing the wheel. Is there a more simple Django-way of doing this?
Warehouse.objects.filter(order__receiver__user=request.user)
you can traverse relations backwards ("reverse lookup") and traverse multiple levels with the double-underscore syntax
https://docs.djangoproject.com/en/1.8/topics/db/queries/#lookups-that-span-relationships
I currently have two different models.
class Journal(models.Model):
date = models.DateField()
from_account = models.ForeignKey(Account,related_name='transferred_from')
to_account = models.ForeignKey(Account,related_name='transferred_to')
amount = models.DecimalField(max_digits=8, decimal_places=2)
memo = models.CharField(max_length=100,null=True,blank=True)
class Ledger(models.Model):
date = models.DateField()
bank_account = models.ForeignKey(EquityAccount,related_name='paid_from')
account = models.ForeignKey(Account)
amount = models.DecimalField(max_digits=8, decimal_places=2)
name = models.ForeignKey(Party)
memo = models.CharField(max_length=100,null=True,blank=True)
I am creating a report in a view and get the following error:
Merging 'ValuesQuerySet' classes must involve the same values in each case.
What I'm trying to do is only pull out the fields that are common so I can concatenate both of them e.g.
def report(request):
ledger = GeneralLedger.objects.values('account').annotate(total=Sum('amount'))
journal = Journal.objects.values('from_account').annotate(total=Sum('amount'))
report = ledger & journal
...
If I try to make them exactly the same to test e.g.
def report(request):
ledger = GeneralLedger.objects.values('memo').annotate(total=Sum('amount'))
journal = Journal.objects.values('memo').annotate(total=Sum('amount'))
report = ledger & journal
...
I get this error:
Cannot combine queries on two different base models.
Anyone know how this can be accomplished?
from itertools import chain
report = chain(ledger, journal)
Itertools for the win!
If you want to do an Union, you should convert these querysets into python set objects.
If it is possible to filter the queryset itself rightly, you should really do that!
Use itertools.chain:
from itertools import chain
report = list(chain(ledger, journal))
Note: you need to turn the resulting object into a list for Django to be able to process it.
I had the same issue. I solved it using the union method combined_queryset = qs1.union(qs2)
Using your example: report = ledger.union(journal)
I have the following models
class Employee(Person):
job = model.Charfield(max_length=200)
class Address(models.Model):
street = models.CharField(max_length=200)
city = models.CharField(max_length=200)
class EmpAddress(Address):
date_occupied = models.DateField()
date_vacated = models.DateField()
employee = models.ForeignKey()
When I build a json data structure for an EmpAddress object using the django serialzer it does not include the inherited fields only the EmpAddress fields. I know the fields are available in the object in my view as I can print them but they are not built into the json structure.
Does anyone know how to overcome this?
Thanks
Andrew
Inheritance of Django models can get a little tricky. Unless you excplicitly require EmpAddress to be subclass of Address, you might just want to duplicate the fields and let duck typing handle the fact that you aren't following traditional object oriented design. E.g:
class Address(models.Model):
street = models.CharField(max_length=200)
city = models.CharField(max_length=200)
class EmpAddress(Address):
street = models.CharField(max_length=200)
city = models.CharField(max_length=200)
date_occupied = models.DateField()
date_vacated = models.DateField()
employee = models.ForeignKey()
Another shot in the dark you might try is to use jsonpickle (I'm one of the developers), which is "smarter" than the standard json module. The latest code has some great new features, thanks to davvid.
Take a look at: http://www.partisanpost.com/2009/10/django-jquery-jqgrid-example-one/1/ as a solution to your problem. The full serializer allows you to drill down into foreignkey relationships as far as you need to go. I wrote a tutorial example of how to use it to integrate django with JqGrid, which provides an example of just what you are faced with. Hope this helps.
John,
This is view code I am using along with the models is;
def address_grid(request):
employeeId = request.GET.get('employeeId')
if request.GET.get('sidx') == '':
order = 'date_occupied'
else:
order = request.GET.get('sidx')
if request.GET.get('sord') == 'asc':
sort_order = ''
else:
sort_order = '-'
order = sort_order + order
if request.GET.get('page'):
paginated = int(request.GET.get('page'))
else:
paginated = 1
items = int(request.GET.get('rows'))
addresses = EmpAddress.objects.filter(employee__id=employeeId)
for add in addresses:
log.write(add.city+'\n') # Field from address object
total = adresses.all().count()
if total % items > 0:
items_sum = 1
else:
items_sum = 0
pages = total / items + items_sum
if paginated > pages:
paginated = 1
addresses = addresses.order_by(order)[paginated-1)*items:paginated*items]
rows = serializers.serialize("json", addresses, indent=4,)
addresses = '{total:%(pages)s, page:%(page)s, records:%(total)s, rows:%(addresses)s' \
% {'pages':pages, 'page':paginated, 'total':total, 'addresses':rows}
log.write(rows+'\n') #json object no Address fields (city is not included)
#even it is present above
return HttpResonse(addresses, mimetype="application/json")
When I print the addresses objects after the
addresses = EmpAddress.objects.filter(employee__id=employeeId)
line I have all of the objects attributes (both the Address and EmpAddress fields).
But when I print the json object I only have the EmpAddress object attributes excluding the Address attributes.