Many2Many field not saving


I have a manyTomany field between my project model and contract model.

class Project(models.Model):
    name = models.CharField(max_length=100, null=True, blank=True)
    last_updated  = models.DateField(null=True, blank=True)
    contracts = models.ManyToManyField('Contract', blank=True)
class Contract(models.Model):
    address = models.CharField(max_length=150, null=True, blank=True, unique=False)

    def __str__(self):
        return str(self.address)

This has been working fine for months. Last night I did a bulk upload from a csv file directly into the database.

Now when I go into the project and select the contract, it doesn’t save anymore.

I’m doing all of this in the admin, so don’t have any views or forms.

I restored the database back to the day before the bulk upload and everything is fine. So something I have done has caused this to break. But I’m not sure why or how to troubleshoot

I guess that something in the upload has messed up the relationship between the models of something.

At any point in this process, are you getting anything in the error logs on the server? That would be the first thing to check.

Side note: It makes no sense to have blank=True on a ManyToManyField, I suggest you remove it.

Hi, Ken.

I’m not getting any errors. Everything seems to be fine.

I’ve run it locally and i get the same problem, but no errors or anything outputted to terminal

I’ll remove the blank=True


For clarification:

You go into the admin for a Project.

You select a Contract for that Project

You save the Project.

The relationship between Project and Contract is not saved.

You have no errors in the server log.

For debugging / diagnostics -

Within the database, check the Project-Contract join table for invalid entries. (Actually, also check to see if the intended entry exists.)

Check the individual rows for project and contract to verify that there’s no invalid data in any column of those two rows.

Check the admin history log to see if the update is shown there.

Double-check your Project admin class.

Run some other verification on the tables on the database looking for invalid or improper data.

But basically, in the absence of any logging information to help, assume nothing. Do not say (or think) that “I know the problem isn’t there” - verify everything.