Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I customize the display of a model using contenttypes in the 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'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 rendered like this:

admin for attachment

In the Content type select, I see this list:

select

The reason Attachment has a GenericForeignKey is because both ProjectA and ProjectB need to access it. I know that ProjectA and ProjectB are identical, but it's a requirement that they are stored in 2 separate tables. How could I made the Attachment class useable from the admin? I know how to use contenttypes from normal views, but from the admin not.

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

Is such a thing possible from the Admin? Will I need to show the user the Object Id column?

like image 938
Geo Avatar asked Jun 13 '11 19:06

Geo


People also ask

Can I use Django admin as frontend?

Django Admin is one of the most important tools of Django. It's a full-fledged application with all the utilities a developer need. Django Admin's task is to provide an interface to the admin of the web project. Django's Docs clearly state that Django Admin is not made for frontend work.

What is the purpose of the Contenttypes framework?

Django includes a contenttypes application that can track all of the models installed in your Django-powered project, providing a high-level, generic interface for working with your models.

What is the purpose of the admin site in a Django project?

The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.


1 Answers

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

my advice will work differently. you will add a little more form in 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 your ProjectA or ProjectB admin and see new admin.

this isn't what you want but it can help you. otherwise you need check first link.

like image 153
mumino Avatar answered Sep 21 '22 07:09

mumino