简体   繁体   中英

how add custom fild to all objects of django model

i have db where is companies and custom users, i want add custom field for different companies. Example: user have only NAME, if company is "EXAMPLE": user have fields only NAME and AGE, elif "VIN": user have only NAME and SIZE and AGE and WORK, else: only name. How add this custom fields and update it. Thanks, sory for my English))

Company 1:

  • name "+add field"

company2: -name -age "+add field"

company 3: -name -age -size "+add field"

I try many to many, many to one, simple fields, dont work how i want. I wand many custom field how i can edit for every user

you must have separated models that refers OneToOne key to user model like this:

class Example(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, )
    name = models.CharField(max_length=30)
    age= models.IntegerField()

class Vin(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, )
    name = models.CharField(max_length=30)
    age= models.IntegerField()
    size= models.IntegerField()
    work= models.CharField(max_length=30)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM