django-models

How to write a Django QuerySet the properly computes average of DateTimeField with grouping?

跟風遠走 提交于 2020-01-02 10:02:54
问题 Here is my Django model: class MyModel(models.Model): a = IntegerField() b = DateTimeField() Here is the QuerySet I execute on this model to find the count, min, max and average b s for each value of a : >>> from django.db.models import Count, Max, Min, Avg >>> MyModel.objects.extra( ... select={'avg': 'AVG(UNIX_TIMESTAMP(b))'} ... ).values('a').annotate( ... count=Count('b'), ... min=Min('b'), ... max=Max('b'), ... ) Here is the result of the QuerySet above: [ {'a': 1, 'count': 5, 'min':

Django admin get_FOO_display

◇◆丶佛笑我妖孽 提交于 2020-01-02 09:52:22
问题 I have model like this: class M(models.Model): CHOICES = (('A', 'Accepted'), ('R', 'Rejected')) status = model.CharField(max_length=1, choices=CHOICES) I register this model in admin by this way: class MAdmin(admin.ModelAdmin): list_display = ('get_status_display', ) But I see field name curried in status column. How I can give normal name Status to column or I make something wrong? And can I sort by this field? 回答1: Set the short_description attribute of your get_status_display callable to

Can i sort query set return objects in order to function result

半城伤御伤魂 提交于 2020-01-02 08:53:34
问题 I am writing a web based music application and I want implement some feature that user can see most favor album in last week-month-year. so this is my model : class album(models.Model): def get_weely_count(): ... def get_monthly_count(): ... def get_yearly_count(): ... class like(models.Model): created_at = models.DateField() albumID = models.ForeignKey(Album) Now I want to receive albums that most liked in last week or last month or last year,I want done some thing like this(but I can not):

Can i sort query set return objects in order to function result

落花浮王杯 提交于 2020-01-02 08:53:31
问题 I am writing a web based music application and I want implement some feature that user can see most favor album in last week-month-year. so this is my model : class album(models.Model): def get_weely_count(): ... def get_monthly_count(): ... def get_yearly_count(): ... class like(models.Model): created_at = models.DateField() albumID = models.ForeignKey(Album) Now I want to receive albums that most liked in last week or last month or last year,I want done some thing like this(but I can not):

Django - ForeignKey field initial value definition in Admin

◇◆丶佛笑我妖孽 提交于 2020-01-02 06:16:48
问题 I have a Person model, which has a ForeignKey field to itself, called mother . When the user goes to the 'add' admin form, I want to define an initial value for mother , in case there is a GET('mother') parameter, or leave it blank, in case there is not. I have actually 2 questions: How to access request inside ModelAdmin ? How to define initial value for a ForeignKey field? In models.py: class Person(models.Model): name=models.CharField() mother=models.ForeignKey('self') In admin.py: class

Making a fairly complex Django model method sortable in admin?

帅比萌擦擦* 提交于 2020-01-02 05:54:04
问题 I have a reasonably complex custom Django model method. It's visible in the admin interface, and I would now like to make it sortable in the admin interface too. I've added admin_order_field as recommended in this previous question, but I don't fully understand what else I need to do. class Book(models.Model): id = models.IntegerField(primary_key=True) title = models.CharField(max_length=200) library_id = models.CharField(max_length=200, unique=True) def current_owner(self): latest

Django tree modeling

被刻印的时光 ゝ 提交于 2020-01-02 05:48:09
问题 I am a completely lost newbie trying to figure out how to make my hard-coded list of links dynamically generated, not only as a noob exercise in learning Django but also so the data can be edited via admin. The important thing is that it looks the exact same when generated via the db as it does now in its hard-coded state. The main content here needs to be represented by models: http://www.drugpolicyreformmovement.com/directory I think that I would first make a 'Category' table of category

django converting old datetime field to new 1.4 datetime with time zone aware in SQLite

家住魔仙堡 提交于 2020-01-02 05:46:06
问题 I have a django 1.3 project and I'm in the process of adding text notifications that will run on a certain schedule. As such, I'm going to need the app to be timezone aware. It looks like django 1.4 handles this pretty well. THe problem is that I am using SQLite for db, and it doesn't store the timezone info, so I have to convert. I'm a django newbie. ANy suggestions on how to convert datetime fields in SQLlite to timezone-aware fields. By the way, when I launch the manage.py shell after

Django models: Set default relative to another field

别来无恙 提交于 2020-01-02 05:34:11
问题 I am building an app using Django 1.10 as backend. Is it possible to set a model field's default relative to another model from the same instance? I specifically need to set second_visit's default to be 3 weeks after the first_visit class SomeModel(models.Model): first_visit = models.DateField() second_visit = models.DateField(default= second_visit_default) def second_visit_default(self): # Set second_visit to 3 weeks after first_visit 回答1: You cannot assign a default value on a field

Django signals file, cannot import model names

半世苍凉 提交于 2020-01-02 05:24:44
问题 I have such file order: project/ app/ models.py signals.py I am keeping signals inside signals.py as it should be. and at the top of the signals.py file, I include myapp models as I do queries in these signals with from myproject.myapp.models import Foo However it doesnt seem to find it, as I run the server or validate from manage.py, it gives this error: from myproject.myapp.models import Foo ImportError: cannot import name Foo I am using Django 1.2.1. 回答1: Most likely you have a circular