Skip to content Skip to sidebar Skip to footer

Valueerror: Needs To Have A Value For Field "" Before This Many-to-many Relationship Can Be Used

Django Version: 1.10.4 Exception Type: ValueError Exception Value: '' needs to have a value for field 'schoolbook' before this

Solution 1:

You cannot access related objects of a many-to-many relation before the instance is saved! The instance must have an id for the intermediate m2n table to point its foreign key (by the name of schoolbook, hence the error message) to:

def save(self, *args, **kwargs):
    if not self.id:
        super(SchoolBook, self).save(*args, **kwargs)
    # process self.parent_subject (should be called ...subjects, semantically)
    super(SchoolBook, self).save(*args, **kwargs)

Post a Comment for "Valueerror: Needs To Have A Value For Field "" Before This Many-to-many Relationship Can Be Used"