Hello, new to django here.
What would be the best practice for registering proxy models (and they fields) to admin?
Hello, new to django here.
I recommend you take a look at this video from Roy Felding, one of the two authors of Two Scoops of Django.
In it he uses proxy models to create multiple types of users. Very informative and easy to follow.
I have my proxy model working, I followed that video and read the book. Still can’t figure out how to register those models to the admin.
I’m not sure that’s accurate @Suttonium
I’ve worked on a project using them just fine with
admin.site.register - what about that isn’t working @vera?
My apologies. It’s early in the morning where I am right now, so I was confusing
@adamchainz Sorry, I got confused too. I managed to show the form at admin, but don’t know how to show the fields witch I have in a EntityMore model (so it stores data on other table, as recommended by the tutorial).
Apologies Vera, I misread your question.
It might help if you share the models involved, for us to provide the answers in proper context. (Personally, I feel more comfortable talking about real situations. I find it helps reduce confusion when trying to apply an answer to a question.)
Ok, this my models.py
from django.db import models from django.utils.translation import gettext_lazy as _ from escuelitaweb.core.models import TimeStamped # Create your models here. class Application(TimeStamped): user = models.ForeignKey("users.Student", models.SET_NULL, blank=True, null=True) name = models.CharField(max_length=64) mail = models.EmailField() class Programs(models.TextChoices): DATASCIENCE = 'DATASCIENCE', 'Data science' program = models.CharField(_('Type'), max_length=50, choices=Programs.choices, default=Programs.DATASCIENCE) # TODO: add accept, reject, ignore methods class DatasienceApplicationManager(models.Manager): def get_queryset(self, *args, **kwargs): return super().get_queryset(*args, **kwargs).filter(program=Application.Programs.DATASCIENCE) class DatasienceApplication(Application): base_program = Application.Programs.DATASCIENCE objects = DatasienceApplicationManager() @property def more(self): return self.datascienceapplicationmore class Meta: proxy = True def save(self, *args, **kwargs): if not self.pk: self.program = Application.Programs.DATASCIENCE return super().save(*args, **kwargs) class DatasienceApplicationMore(TimeStamped): why = models.TextField() interest_data = models.TextField() experience = models.TextField() extra_text = models.TextField()
I want to register the fields from DatasienceApplicationMore in the admin (witch does show the application proxy).
Now, this doesn’t fix this particular issue, but one difference I see between your code and the tutorial is that you don’t have a OneToOne field defined in your “more” class relating it back to the Application model. That might be inhibiting you being able to specify the reverse relationship in your ModelAdmin class.
thank, fixed. Still need to registet the fields to admin.