ValueObjects Django

2

Good morning, I'm starting now with Python and Django and I had a question about the creation of my models. I'd like to create something like:

Person (models.Model)

  • Name
  • ...
  • Address

Address ()

  • Backyard
  • Neighborhood
  • ...

That is, separate the address as a value object to be able to use in more than one model. In the database the fields within the address class will be persisted within the People table and other entities that may appear (Company, Client, "Anyone who can have an address"). So avoid duplicating all address fields for each template you need.

I saw what you would do to set the Meta of the address to abstract and inheriting Address Person. But if I want to do more ValueObjects I will have to inherit from several classes for this, I would like to know if there is a more correct way.

Thank you.

    
asked by anonymous 11.08.2016 / 18:18

3 answers

1

Even though the address is the same among several people, I suggest you leave it as an attribute of the Person class. Unless they live in the same residence / building, we will not have so many equal cases. It will ease the readability and decrease one more relationship between tables in the bank.

In addition, if you are working with a form that is going to send this information, it is difficult to standardize how the user inputs data ('Street xx' vs. 'R. xx', etc.).

    
12.10.2016 / 15:24
1

When I find myself in this kind of situation, I always have two options:

  • Relate the item
  • Create a separate table for the address and add the foreign key on the client.
  • Create an Address table and have the Client table inherit from Address. So it would have the address fields, plus the client fields.
  • 12.10.2016 / 17:19
    0

    I think what you are looking for is a many-to-one relationship. You could create the separate "Address" model and create a foreign key no model "Person"

    class Pessoa(models.Model):
        [...]
        endereco = models.ForeignKey('Endereco', on_delete=models.CASCADE)
    
        
    12.10.2016 / 17:38