[Django]-Weak Entities in Django

3👍

Well, let’s take this example from wikipedia

You have a class Order and a class Product.

You’d then have a class OrderItem which would be the weak entity.

class Order(models.Model):
    some_attributes

class Product(models.Model):
    some_other_attributes

class OrderItem(models.Model)
    order = models.ForeignKey(Order)
    product = models.ForeignKey(Product)
    other_attributes

    class Meta:
        unique_together = (order, product)

the unique_together meta property would make sure each OrderItem’s won’t have more than a database entry where both these values are repeated.

I reckon this may not match exactly what it you’re looking for, but it may be a start. If you provide more details on what’s you’re trying to accomplish, perhabs I can help with some table tweaking or even query examples for getting data using this approach.

edit:
You are correct, there is no such field as weaker entity field. My sugestion is that you treat the week model as you would any other. And link it to the Course model, like so:

 class Course(models.Model):
      title = models.CharField()
      description = models.CharField()
      etc..

 class CourseWeek(models.Model):
      course = models.ForeignKey(Course)
      week_number = models.IntegerField()
      topic = models.CharField()
      slideshow = models.FileField()

      class Meta:
         unique_together = ('course' , 'week_number')

Hope this helps 🙂

Leave a comment