简体   繁体   中英

Many to Many relationship Django Models with Extra Attributes

i'm a beginner in Django development (and MVC programming in general!) considering the following example how could I translate these entities into models?

USER (id, name, surname)

ENGLISH_CERTIFICATION (id, code, name)

USER_CERTIFICATION (id, id_user, id_english_certification, date)


class User(models.Model):
    name = models.CharField(max_length=64)
    surname = models.CharField(max_length=64)
    ???

class EnglishCertification(models.Model):
    code= models.CharField(max_length=2)
    name = models.CharField(max_length=64)
    ???

Where i put the relationships and the field "date"? Thank you!

If you want to create models that exactly represent the entities as you described, you could simply create an additional Model similar to:

class UserCertification(models.Model):
    user = models.ForeignKey('User', on_delete=models.CASCADE)
    english_certification = models.ForeignKey('EnglishCertification', on_delete=models.CASCADE)
    date = models.DateField()

Of course you will need to adapt the code above depending on your needs. For more information you can have a look at:

https://docs.djangoproject.com/en/2.1/topics/db/examples/many_to_one/

Hope this helps

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