2

I am building a web application with Django, I did the design app with UML2. i read that association class concept does not exist in object oriented programming languages, is that true ?? thank you.

class diagram

class diagram

Mustafa
  • 977
  • 3
  • 12
  • 25
kamel dev
  • 23
  • 4

1 Answers1

1

No. You can implement that model relationship design as follows:

class Society(models.Model):
    name = models.CharField(max_length=100)

class User(models.Model):
    name = models.CharField(max_length=100)
    societies = models.ManyToManyField(Society, through='Employment', related_name='users', blank=True)

class Employment(models.Model):
    class Meta:
        unique_together = [('user', 'society')]
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    society = models.ForeignKey(Society, on_delete=models.CASCADE)
    salary = models.IntegerField()
schillingt
  • 13,493
  • 2
  • 32
  • 34
  • can you please clarify to me the need of 'societies = models.ManyToManyField' in User Class. Don't we just need to add user and society to Employment Class ?. thank you – kamel dev Mar 19 '20 at 18:47
  • 2
    You can do that too. However, your diagram shows a clear relationship between the two so I figured you'd want the ability to do `user.societies.all()`. Here are the docs that explain the ManyToManyField - https://docs.djangoproject.com/en/3.0/ref/models/fields/#manytomanyfield – schillingt Mar 19 '20 at 18:51
  • thank you for your clarifications, really helped me :) – kamel dev Mar 19 '20 at 18:59