0

py like the following

class User_(models.Model):
user_name = models.CharField(max_length=50)
user_password = models.CharField(max_length=50)
user_admin = models.BooleanField(False)

class Project_(models.Model):
project_name = models.CharField(max_length=20)
users = models.ManyToManyField(User_)
project_path = models.FilePathField()

and now I tried to use the many_to_many field users in the following

    usr = User_.objects.get(user_name= userName) #it retrieves the correct object
    newProject = Project_(project_name=projectName)
    newProject.save()
    newProject.users.add(usr) #here is the error 
    newProject.save()

The error raised is: "Cannot resolve keyword 'user' into field. Choices are: id, project_, user_"

I could not fix that error, Any help? Thanks.

N0rA
  • 612
  • 1
  • 7
  • 27
  • Are you sure you copied the error message correctly? It seems to reference a keyword 'user' which doesn't exist on the line you indicated. – cberner Jun 25 '12 at 18:40

1 Answers1

1

The problem is your table names... remove the trailing underscores and it'll work fine.

Django creates an intermediary table for m2m fields called something like APP_MODEL1_MODEL2, and due to your underscores, there will be a double underscore in there which is confusing the ORM.

Greg
  • 9,963
  • 5
  • 43
  • 46