How to configure model display using contenttypes in admin?

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 register all models for the administrator, and I do not register the group, user, and site. The fact is that when I get access to the attachment in the admin, I see that it looks like this:

admin for attachment

In the Content Type field, select this list:

select

The reason for Attachment is GenericForeignKey, because both ProjectA and ProjectB must access it. I know that ProjectA and ProjectB are identical, but it is a requirement that they are stored in two separate tables. How could I make the Attachment class useful from the administrator? I know how to use contenttypes from regular views, but not from the administrator.

In the Attachment class, I would only like to select for Project A or Project B, and then a list of all Project A or all Project B, followed by the file I want to add.

Is this possible from the administrator? Do I need to show the object identifier column to the user?

+6
django django-models django-admin django-contenttypes
Jun 13 2018-11-11T00:
source share
2 answers

If I'm not mistaken, you want this. http://code.google.com/p/django-genericadmin/

My advice will work differently. you will add a little more form to ProjectA, ProjectB as inline. in your admin.py

 from django.contrib import admin from django.contrib.contenttypes import generic from myproject.myapp.models import Attachment, ProjectA, ProjectB class Attachmentline(generic.GenericTabularInline): #or generic.GenericStackedInline, this has different visual layout. model = Attachment class ProjectAdmin(admin.ModelAdmin): inlines = [ Attachmentline, ] admin.site.register(ProjectA, ProjectAdmin) admin.site.register(ProjectB, ProjectAdmin) 

Go to your ProjectA or ProjectB administrator and see the new administrator.

this is not what you want, but it can help you. otherwise, you need to check the first link.

+8
Jun 14 2018-11-11T00:
source share

You should notice that

"know that ProjectA and ProjectB are identical, but it is a requirement that they be stored in two separate tables"

not entirely correct. All data is stored in your app_projecta table, and (only) some pointers are stored in the app_projectb table. If you are already following this path, I would suggest starting with this:

 class App(models.Model): name = models.CharField(max_length=100) class Project(models.Model): name = models.CharField(max_length=100) app = models.ForeignKey(App) class ProjectA(Project): pass class ProjectB(Project): pass class Attachment(models.Model): project = models.ForeignKey(Project) file = models.FileField(upload_to=".") 

This will bring you closer to where you want to get ...

+1
Jun 13 2018-11-11T00:
source share



All Articles