Question

Can somebody please explain me how to represent a weak entity relationship in django?

i searched for it in django's documentation, but couldn't find. I even searched on google and stackoverflow before posting a question here.

In case its not possible to represent a weak entity relationship, then please let me know what is the most appropriate alternative to it in django

Update:

I was developing an e-learning site, so there's a Course class with attributes like title, description, creator, etc. and I want to add a weak entity called "Week" (to store week-wise content for the course) which would be attached to this strong entity called Course, and I wanted course_id from Course class and week_no from Week class to act as a primary key for each entry in Week class

Was it helpful?

Solution

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 :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top