contacts.contacts_to_users: Accessor for field ‘user’ clashes with related field ‘User.contacts_to_users_set’. Add a related_name argument to the definition for ‘user’.

Quote

Model looks like:

class CONTACTS_TO_USERS(models.Model):
   contact = models.ForeignKey('contacts.Contact')
   user = models.ForeignKey('auth.User')
   creator = models.ForeignKey('auth.User')
   destroyer = models.ForeignKey('auth.User')
   date_start = models.DateTimeField(auto_now_add=True)
   date_end = models.DateTimeField(default=datetime.datetime(2999,12,31))
   class Meta:
      db_table='contacts_m2m_users'
[cmuser@cmlin02 cmdb]$ python manage.py syncdb
Error: One or more models did not validate:
contacts.contacts_to_users: Accessor for field 'user' clashes with related field 'User.contacts_to_users_set'. Add a related_name argument to the definition for 'user'.
contacts.contacts_to_users: Accessor for field 'creator_id' clashes with related field 'User.contacts_to_users_set'. Add a related_name argument to the definition for 'creator_id'.

SOLUTION

class CONTACTS_TO_USERS(models.Model):
   contact = models.ForeignKey('contacts.Contact')
   user = models.ForeignKey('auth.User')
   creator_id = models.ForeignKey('auth.User', related_name='creator')
   destroyer_id = models.ForeignKey('auth.User', related_name='destroyer')
   date_start = models.DateTimeField(auto_now_add=True)
   date_end = models.DateTimeField(default=datetime.datetime(2999,12,31))
   class Meta:
      db_table='contacts_m2m_users'

 

In Django: create a user, generate the password, add to a group.

email_address = "foo@bar.com"
admin_uid = "foo"
new_password = User.objects.make_random_password()
# Create the user.
new_user = User(username=admin_uid, email=email_address)
new_user.set_password(new_password)
new_user.is_active = True
new_user.save()

# Add the user to the brand_specific_admin group
g = Group.objects.get(name='brand_specific_admin')
g.user_set.add(new_user)