django-models

Django: Why is Foo.objects.extra(…) So Much Faster Than Foo.objects.raw?

匆匆过客 提交于 2021-02-19 01:59:28
问题 So I am trying to optimize a fairly odd query, but this is a legacy database so I make do with what I have. These are the queries I am trying. They provide the same output at this point. w is my queryset. def future_schedule(request): past = datetime.date.today()-datetime.timedelta(days=730) extra_select = { 'addlcomplete': 'SELECT Complete FROM tblAdditionalDates WHERE Checkin.ShortSampleID = tblAdditionalDates.ShortSampleID', 'addldate': 'SELECT AddlDate FROM tblAdditionalDates WHERE

Django: query spanning multiple many-to-many relationships

最后都变了- 提交于 2021-02-18 22:36:50
问题 I've got some models set up like this: class AppGroup(models.Model): users = models.ManyToManyField(User) class Notification(models.Model): groups_to_notify = models.ManyToManyField(AppGroup) The User objects come from django's authentication system. Now, I am trying to get all the notifications pertaining to the groups that the current user is a part of. I have tried.. notifications = Notification.objects.filter(groups_to_notify=AppGroup.objects.filter(users=request.user)) But that gives an

Create object only if other object is successfully created

回眸只為那壹抹淺笑 提交于 2021-02-18 19:09:57
问题 I am fairly new to Django and unfamiliar with the best practices for this situation (in any framework/language, not just python/django). The situation is that when a user first registers on my site, I want to create an "organization" for them if it doesn't exists, and then subsequently create a user for them, which references the organization. I never want to insert one without the other, but I need to create the organization first so that the organization UUID can be saved for each user.

Create object only if other object is successfully created

痞子三分冷 提交于 2021-02-18 19:04:02
问题 I am fairly new to Django and unfamiliar with the best practices for this situation (in any framework/language, not just python/django). The situation is that when a user first registers on my site, I want to create an "organization" for them if it doesn't exists, and then subsequently create a user for them, which references the organization. I never want to insert one without the other, but I need to create the organization first so that the organization UUID can be saved for each user.

Raise validation error in inline field Django

不羁的心 提交于 2021-02-18 18:54:38
问题 I have a model that contains a TabularInline , and I want to raise a validation error when a condition is not valid. My parent model: @admin.register(Even) class EventAdmin(admin.ModelAdmin): list_display = ['id', 'title'] list_display_links = ['id', 'title] inlines = [EventSpecialPriceInline] And my TabularInline: class EventSpecialPriceInline(admin.TabularInline): model = EventSpecialPrice extra = 0 can_delete = True The error I want to raise is when a price of a row is negative

Model and Permissions

二次信任 提交于 2021-02-18 16:54:25
问题 I'm currently learning python/django As a Tutorial for myself I have done that: create user --> ok login user created --> ok create a new model called 'Article' with title and content as a properties --> ok create several 'Article' instances --> ok I display all the articles at the root of the webapp --> Last step is: in order to manipulate permission, only display some article depending on which user is logged with permissions. like if user A: then display only the article with odd ids (that

Django cascade delete and post_delete signal

℡╲_俬逩灬. 提交于 2021-02-18 11:55:08
问题 In my application post_delete signals being recorded in a specific model and when it was removed. class A(models.Model): ... class B(models.Model): a = models.ForeignKey('A') class C(models.Model): b = models.ForeignKey('B') def log_delete(sender, instance, **kwargs): logging post_delete.connect(log_delete, sender = A) post_delete.connect(log_delete, sender = C) When you delete an instance of A cascade delete occurs removing B and C instances. How can I disable signal for child instances on

Auto Populate Slug field django

久未见 提交于 2021-02-18 11:10:14
问题 I have a model defined and over 100+ entries of data in my DB. I would like to auto populate a slug field and see it show up in the admin, since adding new entries for 100+ fields is not something I would like to do. AutoSlug() field doesnt seem to be working when I add it to my model and make the migrations, prepopulated_fields = {'slug': ('brand_name',)} does not work using it within my admin.py and as well I have tried to add the default field on the slug as my desired field name within

Auto Populate Slug field django

删除回忆录丶 提交于 2021-02-18 11:05:39
问题 I have a model defined and over 100+ entries of data in my DB. I would like to auto populate a slug field and see it show up in the admin, since adding new entries for 100+ fields is not something I would like to do. AutoSlug() field doesnt seem to be working when I add it to my model and make the migrations, prepopulated_fields = {'slug': ('brand_name',)} does not work using it within my admin.py and as well I have tried to add the default field on the slug as my desired field name within

Auto Populate Slug field django

感情迁移 提交于 2021-02-18 11:03:59
问题 I have a model defined and over 100+ entries of data in my DB. I would like to auto populate a slug field and see it show up in the admin, since adding new entries for 100+ fields is not something I would like to do. AutoSlug() field doesnt seem to be working when I add it to my model and make the migrations, prepopulated_fields = {'slug': ('brand_name',)} does not work using it within my admin.py and as well I have tried to add the default field on the slug as my desired field name within