Django 1.6 AbstractUser m2m validation error

I have no errors with Django 1.5.4 (stable), but when I tested my application on Django 1.6 beta 4 from the official tar.gz, I got an error with validation models at startup.


models.py

from django.contrib.auth.models import AbstractUser, User class ShopUser(AbstractUser): model_car = models.CharField(max_length=200) date_car = models.DateField() description = models.TextField(blank=True, db_index=True) manager = models.ForeignKey(User) 

This is the console.py runningerver management log :

 Validating models... Unhandled exception in thread started by <function wrapper at 0x2d941b8> Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 93, in wrapper fn(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 97, in inner_run self.validate(display_num_errors=True) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 312, in validate raise CommandError("One or more models did not validate:\n%s" % error_text) django.core.management.base.CommandError: One or more models did not validate: adminka.shopuser: Accessor for m2m field 'groups' clashes with related m2m field 'Group.user_set'. Add a related_name argument to the definition for 'groups'. adminka.shopuser: Accessor for m2m field 'user_permissions' clashes with related m2m field 'Permission.user_set'. Add a related_name argument to the definition for 'user_permissions'. auth.user: Accessor for m2m field 'groups' clashes with related m2m field 'Group.user_set'. Add a related_name argument to the definition for 'groups'. auth.user: Accessor for m2m field 'user_permissions' clashes with related m2m field 'Permission.user_set'. Add a related_name argument to the definition for 'user_permissions'. 
  • python -c "import django; print django.get_version ()"
    1.6b4

What needs to be done to solve this problem?

+6
source share
3 answers

You must declare AUTH_USER_MODEL on settings.py . In your case:

 AUTH_USER_MODEL = 'your_app.ShopUser' 
+15
source

For me, this was fixed in Django 1.6.5 by changing this:

 from django.contrib.auth.models import AbstractUser class CustomUser(AbstractUser): pass 

:

 from django.contrib.auth.models import AbstractBaseUser class CustomUser(AbstractBaseUser): pass 

No other changes needed in Django 1.6.5. The @Stormlifter suggestion makes sense, but I use this CustomUser with this stuff for OAuth2 via python-social-auth:

 $ pip freeze Django==1.6.5 Markdown==2.4.1 MySQL-python==1.2.5 PyJWT==0.2.1 South==1.0 basicauth==0.2 boto==2.28.0 django-filter==0.7 django-guardian==1.2.0 django-storages==1.1.8 djangorestframework==2.3.14 httplib2==0.9 oauth2==1.5.211 oauthlib==0.6.3 python-memcached==1.53 python-oauth2==0.7.0 python-openid==2.2.5 python-social-auth==0.2.1 requests==2.4.1 requests-oauthlib==0.4.1 shortuuid==0.4.2 six==1.7.2 wsgiref==0.1.2 

My new user user will do more than the default user and he needs to be AUTH_USER_MODEL='myapp.CustomUser' in settings.py , as @jordiburgos suggested.

Hope this helps!

+1
source

You want to inherit from AbstractBaseUser not from AbstractUser .

However, it seems that you just want to keep some additional information for users, I would not recommend using a custom model for this. Just use a relationship.

 class ShopUser(models.Model): model_car = models.CharField(max_length=200) date_car = models.DateField() description = models.TextField(blank=True, db_index=True) user = models.OneToOneField(User) 

You can do this with ForeignKey if you need several ShopUser to communicate with one user, but I think OneToOne makes the most sense.

As the django docs say:

Specifying a user model of user

AbstractBaseUser

0
source

Source: https://habr.com/ru/post/955040/


All Articles