django-queryset

Alias Name in Query Set

痞子三分冷 提交于 2019-12-07 13:04:56
问题 I'm using following code set to pull data from specific columns of the table... cityList = City.objects.using(settings.DATABASE_CONF).filter( status=1).values('city_name_en', 'city_id') How can I set an alias for the city_name_en column? I have two other columns with city_name_fr city_name_de but the client can understand only city_name . 回答1: I think giving alias in Django query is not possible. However you can refer to this answer. 回答2: You can annotate the fields as you want, with the F

Django queryset aggregate by time interval

落花浮王杯 提交于 2019-12-07 12:11:04
问题 Hi I am writing a Django view which ouputs data for graphing on the client side (High Charts). The data is climate data with a given parameter recorded once per day. My query is this: format = '%Y-%m-%d' sd = datetime.datetime.strptime(startdate, format) ed = datetime.datetime.strptime(enddate, format) data = Climate.objects.filter(recorded_on__range = (sd, ed)).order_by('recorded_on') Now, as the range is increased the dataset obviously gets larger and this does not present well on the graph

How do I generate and display Django querysets from the results of another queryset?

爷,独闯天下 提交于 2019-12-07 10:39:08
问题 For example, let's say I have the following models: class Group(models.Model): group_name = models.CharField(max_length=20) class Person(models.Model): group = models.ForeignKey(Group) name = models.CharField(max_length=50) I want to list all groups, and for each group list the people in the group. Group A: Person1, Person2, Person3 Group B: Person4, Person5, Person6 I get stuck at Group.objects.all(), which will only return a queryset containing the Group objects that I can cycle through in

Django full text search order by relevance

萝らか妹 提交于 2019-12-07 05:31:57
问题 I am using the Django query filter __search to perform a full text search e.g MyModel.objects.filter(title__search = 'some title') How do I get it to order by relevance, as currently it seems to be ordering alphabetically? Specifically I would like search results where the title was some title to appear first before something that had the title a different but contains some title . edit: What I've noticed is that on the model definition for MyModel I have: class Meta: ordering = ['title'] If

how to use __year and __in in the same query?

你离开我真会死。 提交于 2019-12-07 05:17:31
问题 So here's what I'm trying to do. I've a list with years inside, for instance years = [2002, 2003, 2004] and I've a SomethingModel with a DateField I want to do a query that will return me all the objects that belongs to that year: I known this work: SomethingModel.objects.filter(date__year=2003) SomethingModel.objects.filter(date__in=[list with dates]) So I've tried this: SomethingModel.objects.filter(date__year__in=years) but this return me this error: FieldError: Join on field 'date' not

How to aggregate computed field with django ORM? (without raw SQL)

这一生的挚爱 提交于 2019-12-07 04:02:56
问题 I'm trying to find the cumulated duration of some events, 'start' and 'end' field are both django.db.models.DateTimeField fields. What I would like to do should have been written like this: from django.db.models import F, Sum from my.models import Event Event.objects.aggregate(anything=Sum(F('start') - F('end'))) # this first example return: # AttributeError: 'ExpressionNode' object has no attribute 'split' # Ok I'll try more SQLish: Event.objects.extra(select={ 'extra_field': 'start - end' }

Django - getting object by its related fields' IDs

时光毁灭记忆、已成空白 提交于 2019-12-07 03:28:26
Let's assume we have following models: from django.db import models class Foo(models.Model): name = models.CharField(max_length=50) class Bar(models.Model): name = models.CharField(max_length=50) class Zomg(models.Model): foo = models.ForeignKey(Foo) bar = models.ForeignKey(Bar) In Zomg model foo and bar fields serve as composite key, that is, for any pair of ( foo , bar ) there is only one Zomg object. In my project I need to update thousands of Zomg records from time to time, so efficiency of finding records is important. The problem is that I cannot just pass foo_id and bar_id to Zomg

Use method other than __unicode__ in ModelChoiceField Django

倾然丶 夕夏残阳落幕 提交于 2019-12-07 03:25:08
问题 I'm working on some forms in Django. One field is a ForeignKey in the model, so represented as a ModelChoiceField in the form. The ModelChoiceField currently uses the __unicode__ method of the model to populate the list, which isn't my desired behavior. I'd like to be able to use another method of the model. From the docs, it looks like I can force my own QuerySet , but I can't see how this would help me use a method other than __unicode__ . I'd really rather avoid divorcing this from the

Django JOIN query without foreign key

六月ゝ 毕业季﹏ 提交于 2019-12-07 03:22:37
问题 Is there a way in Django to write a query using the ORM, not raw SQL that allows you to JOIN on another table without there being a foreign key? Looking through the documentation it appears in order for the One to One relationship to work there must be a foreign key present? In the models below I want to run a query with a JOIN on UserActivity.request_url to UserActivityLink.url. class UserActivity(models.Model): id = models.IntegerField(primary_key=True) last_activity_ip = models.CharField

Translating query with JOIN expressions and a generic relation to Django ORM

早过忘川 提交于 2019-12-07 02:48:28
class Business(models.Model): manager = models.ForeignKey(User, on_delete=models.CASCADE) #... class Event(models.Model): business = models.ForeignKey(Business, on_delete=models.CASCADE) text = models.TextField() when = models.DateTimeField() likes = GenericRelation('Like') class Like(models.Model): person = models.ForeignKey(User, on_delete=models.CASCADE) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = GenericForeignKey('content_type', 'object_id') date = models.DateTimeField(auto_now=True) So I have this structure in models.py . Here