django-contenttypes

Change the polymorphic content type of a django model instance

送分小仙女□ 提交于 2020-01-15 09:58:08
问题 If I have a polymorphic model: class Father(polymorphic.model.PolymorphicModel) and an inheritor class with no extra fields: class Child(Father) When I have an instance of Father, how can I convert it to a Child instance? What I have tried is: foo = Father.objects.get(pk=1) # foo is just a Father, no record in Child database table. foo.polymorphic_ctype = ContentType.objects.get(app_label='myapp', model='child') foo.save() But nothing changes. I want foo to be a Child object and need to have

How can I customize the display of a model using contenttypes in the admin?

孤人 提交于 2020-01-13 09:52:11
问题 I have these models: class App(models.Model): name = models.CharField(max_length=100) class ProjectA(models.Model): name = models.CharField(max_length=100) app = models.ForeignKey(App) class ProjectB(ProjectA): pass class Attachment(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() project = generic.GenericForeignKey("content_type","object_id") file = models.FileField(upload_to=".") I'm registering all the models for the admin, and I'm

Django - Proper exception for ContentType.get_object_for_this_type()

a 夏天 提交于 2020-01-06 21:58:30
问题 I am learning to use the ContentType framework and need some help raising an exception for get_object_for_this_type() . According to the code: Returns an object of this type for the keyword arguments given. Basically, this is a proxy around this object_type's get_object() model method. The ObjectNotExist exception, if thrown, will not be caught, so code that calls this method should catch it. I am wondering if there is a way to do this without bringing in the actual models being referenced.

Getting a “The following content types are stale and need to be deleted” when trying to do a migrate. What does this mean, and how can I solve it?

懵懂的女人 提交于 2020-01-01 08:25:11
问题 This is my models.py: class Notification(models.Model): user = models.ForeignKey(User) createdAt = models.DateTimeField(auto_now_add=True, blank=True) read = models.BooleanField(default=False, blank=True) class Meta: abstract = True class RegularNotification(Notification): message = models.CharField(max_length=150) link = models.CharField(max_length=100) class FNotification(Notification): # same as Notification pass When I do python manage.py makemigrations , this is what it says: Migrations

Django Models Polymorphism and Foreign Keys

混江龙づ霸主 提交于 2019-12-25 01:17:22
问题 I have 3 different kinds of users in my application. Customer that goes on, finds appointments, and books appointments. Individual that can create appointments for customers to sign up for, and collect payments for the appointments Organization that can create appointments for customers to sign up for, collect payments for appointments, and provide links to individual providers that are employed in the organization (i.e. users in group 2 above) I see types 1 and 2 having some overlap in that

Filter Models based on content type django 1.7

好久不见. 提交于 2019-12-24 13:27:34
问题 My models.py looks like class OneTimeEvent(models.Model): title = models.CharField(max_length = 160) location = models.CharField(max_length = 200, blank = True) event_date = models.DateTimeField('event date',blank = True, null = True) price = models.IntegerField(max_length = 20, blank = True, null = True) seats = models.IntegerField(max_length = 20, blank = True, null = True) abstract = models.TextField() event_plan = models.TextField() available_seats = models.IntegerField(max_length = 20,

create django permissions, error: ContentType matching query does not exist

前提是你 提交于 2019-12-23 15:26:03
问题 I am trying to add two groups and give them permissions to my Django project. But I keep getting the error: ContentType matching query does not exist. I am running: Django 1.5.4 Python 2.7.3 South 0.8.2 PostreSQL 9.3 Here is my code: import django from django.contrib.auth.models import Group, Permission from django.contrib.contenttypes.models import ContentType from .models import Flavor def add_groups(): # Create User Groups special_users = Group(name='Special Users') special_users.save()

How can I restrict Django's GenericForeignKey to a list of models?

扶醉桌前 提交于 2019-12-17 03:24:15
问题 Is there a way of telling django that a model having a contenttypes GenericForeignKey can only point to models from a predefined list? For example, I have 4 models: A, B, C, D and a model X that holds a GenericForeignKey. Can I tell X that only A & B are allowed for the GenericForeignKey? 回答1: For example, your apps are app and app2 and there are A, B models in app and there are C, D models in app2. you want to see only app.A and app.B and app2.C from django.db import models class TaggedItem

update_all_contenttypes seemingly not working with Django 1.8

China☆狼群 提交于 2019-12-12 12:22:28
问题 I was getting the following error upon running a certain function: django.contrib.contenttypes.models.DoesNotExist: ContentType matching query does not exist. Based upon ContentType matching query does not exist on post_syncdb I tried doing from django.contrib.contenttypes.management import update_all_contenttypes update_all_contenttypes() # make sure all content types exist at the beginning of that function. Of course, as ImportError: cannot import name update_all_contenttypes mentions,

How can I customize the display of a model using contenttypes in the admin?

时光怂恿深爱的人放手 提交于 2019-12-05 06:54:24
I have these models: class App(models.Model): name = models.CharField(max_length=100) class ProjectA(models.Model): name = models.CharField(max_length=100) app = models.ForeignKey(App) class ProjectB(ProjectA): pass class Attachment(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() project = generic.GenericForeignKey("content_type","object_id") file = models.FileField(upload_to=".") I'm registering all the models for the admin, and I'm unregistering Group, User and Site. The thing is, when I access the Attachment in the admin, I see it