Convert an Existing Model in to a proxy model.

I have 2 question I need to clarify before going forward.

I have few Models which I have been keeping for a some time which is updated daily, however now they seems redundant. so I am thinking of turning them in to proxies.

First question. Will they work as they had if I update them into proxies, do i need to update my whole codebase relating to those models, which those models also have relationships to other models …

My second question is, I have these 2 Model which share via a relation ship,
for example:

Class Captain:
  name = models.charfiedl(max_length=100)

Class Boat:
  name = models.charfiedl(max_length=100)
  captain = models.OneToOne('Captain', ...)

To:

Class Captain:
  name = models.charfiedl(max_length=100)
  Boat= models.OneToOne('Boat', ...)

Class Boat:
  name = models.charfiedl(max_length=100)

If I update this relationship what things do i need keep in mind.

thanks.

Can you clarify what you’re trying to accomplish by doing this?

A proxy model is something added to an existing model, it doesn’t replace it and it’s not a substitute for one, so what you’re trying to describe here doesn’t make sense.

Sorry I did not make it clear on the above question.

I have a An AbstractUser model and I have 2 other models which shares the same information as the AbstractUser, now come to think of it, I don’t need those “extra 2 models”. so I am thinking of making the "other 2 models " to proxies of the AbstractUser model. However I have the “extra 2 models” related to other models,
My question is what implications will I have to the relationships the “other 2 models” share.

and regarding my second question. if you have any feedback please share.

thanks.

I don’t think you’re using the word “proxy” the way that Django does. There is a very specific type of model in Django known as a Proxy model, and I don’t think that’s what you’re trying to describe here.

So let me phrase the question a different way.

What do you mean by the term “proxy” here?

What is the functional issue that you’re trying to address?

What is your underlying objective for whatever it is you’re trying to do?

Here is my Diagram:

class Person(AbstractUser):
    pass

class Captain(models.Model):
    name = models.OneToOneField(Person, on_delete=models.CASCADE)
    contact = models.CharField(max_length=200)
    boat = models.ForeignKey(Boat, on_delete=models.CASCADE, null=True, blank=True)
    ....

class Technician(models.Model):
    name = models.OneToOneField(Person, on_delete=models.CASCADE)
    contact = models.CharField(max_length=200)
    engine = models.ForeignKey(Engine, on_delete=models.CASCADE, null=True, blank=True)
    ......

and I am thinking of changing the above code to:

class Person(AbstractUser):
    pass

class Captain(Person):
    class Meta:
        proxy = True

class Technician(Person):
    class Meta:
        proxy = True

and my first question was, with this update, the implications theForeignKeys relationships in the Captain and Technician have, is it better to continue with my existing Model structure or will changing these models have any benefits.

I am asking this because in the long run, more Models will be coming up relating to the Person Model

thanks.

My gut reaction is that you absolutely do not want to convert these models to proxy models.

First, quoting directly from the docs for Model Inheritance:

  1. Finally, if you only want to modify the Python-level behavior of a model, without changing the models fields in any way, you can use Proxy models.

[emphasis added]

Also, I strongly encourage you to completely read those Proxy model docs to really understand what they can and cannot do.

I could be wrong here - you may have a use-case for this, but I have no way to tell because you have not yet described the why behind these questions. You keep referencing the “what you want to do”, but that doesn’t give me enough information to fully answer your question.

Why I wanted to change the models to proxies, is because I do not want to make extra models in the DB for User Roles, (captain, technician, crew,…), which all share some common fields.

However after reading through the Docs, and as per your suggestion, I have decided Not to alter the existing models. since it will lead to more unnecessary complications in the future.

happy coding :slight_smile:

This is the key point here - there are some common fields, but not all.

Once you’ve identified the fact that not all of the fields are identical, you’ve immediately shown that Proxy models are not the answer.

If you’re looking to reduce the size of your models.py file, you could use either abstract models or multi-table inheritance in your definitions.

But:

This should not be an objective - or even a concern.

Modern database engines exist to optimize access among multiple tables. In the general case, you’re likely to be better off with more tables rather than fewer. Let the database do what it does best.