1👍
Instead of setdefault
, you can override the on_delete
parameter:
class UserWithProfileField(models.ForeignKey):
def __init__(self, *args, **kwargs):
kwargs['to'] = UserWithProfile
kwargs['on_delete'] = models.CASCADE
super().__init__(*args, **kwargs)
regardless what the user will now use for to=…
or on_delete=…
, it will use UserWithProfile
and CASCADE
.
Strictly speaking one can of course still try to alter the attributes of the ForeignKey
, but that is more complicated, especially since Django constructs a ForeignObjectRel
object to store relation details.
Note that a proxy model [Django-doc] is not used to add exta fields to the model. THis is more to alter the order, etc. and define new/other methods.
1👍
I don’t get the invariants you are starting out with:
- It’s irrelevant whether you want to delete references to
User
orUserWithProfile
since these are the same table? - You cannot police what other tables and model authors do and in which way shape or form they point to this table. If they use any kind of ForeignKey that’s fine, but they could also point to the table using an unconstrained (integer?) field.
Could you make a test that bootstraps the database and everything, iterates over all models (both in this app and others) and checks every ForeignKey that is there to see if it is pointing to this model and it is setup correctly? That should serve the intended goal I believe.
- [Django]-Custom Django admin URL + changelist view for custom list filter by Tags
- [Django]-Why is the first "hello world" example in the django book not working?
- [Django]-Advanced search using HayStack + Solr in Django?
- [Django]-Nested urls are not working with Django REST framework
- [Django]-Retrieving POST data from jQuery in Django without a form