Q:

Django: assert 'Many-to-many' relation exists in test

I am writing tests for my project, but I've run into a problem when trying to verify the existence of a 'ManyToMany' relationship.  
The test concerns the following two models, that are linked together with a ManyToMany
Models:
class Project(models. Model):
    (...)
    linked_attributes = models. ManyToManyField(attributes, blank=True)

class Attributes(models. Model):
    (...)
    class linked_projects = models. ManyToManyField(Project, blank=True)

In my test I wanted to verify that the form created a new many to many relationship.  I created the assert on the last line, based on some example code, but it doesn't seem to be working.  
Test:
class ProjectTest(TestCase):
    (...)
    form_data = {'linked_attributes' : self.attribute}
    form = ProjectForm(data=form_data, project=self.project, instance=self.project)

   self.assertTrue(Project.attributes_set.filter(pk=self. Project.pk).exists())

Does anyone know what I am doing wrong? 

A:

Your model structure is wrong.  You should only define the many-to-many on one side of the relationship; the other side is accessed via the reverse relationship. 
Also, your assertion is wrong.  You need to query the linked attributes via the project instance, not the Project class as a whole. 
Finally, do you actually have some code before that assertion to validate and save the form?  Otherwise nothing will have changed. 
So:
self.assertTrue(form.is_valid())
saved_project = form.save()
self.assertTrue(saved_project.attributes_set.exists())

