admin.py
class NetUserInline(admin.StackedInline):
model = NetUser
class UserProfileAdmin(UserAdmin):
inlines = [NetUserInline, ]
admin.site.register(User, UserProfileAdmin)
models.py
class NetUser(AbstractUser):
Net_30 = models.BooleanField(default=False)
site.register(NetUser)
im getting error like this.
django.core.management.base.CommandError: One or more models did not validate:
accounts.netuser: Accessor for m2m field 'groups' clashes with related m2m field 'Group.user_set'. Add a related_name argument to the definition for 'groups'.
accounts.netuser: 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'.
The Django User model is automatically part of any Django project. That is to say that the User model will create all database tables that are part of it at the same time you run syncdb. You have subclassed AbstractUser for your NetUser class, meaning that all the fields that are part of AbstractUser are part of NetUser as well. So, right now your database has tables such as this...
User
username
password
email
...
NetUser
username
password
email
...
net_30
As part of Django's user administration, database tables are created that manage what groups/permissions users have within your application(s). Right now it appears that this system is attempting to connect to both the standard User model and your NetUser model. I'd assume this is because by subclassing AbstractUser Django attempts to create all relations to that user model. At the same time, if you haven't specified that you are using a custom user model then Django will be attempting to do the same to the standard user model.
The documentation states you must specify what you are using for a user model if not the default by adding AUTH_USER_MODEL = 'Accounts.NetUser' to your settings.py file. This tells your Django project that you are using a custom User model and not connect to the standard User model. Right now both are happening.
Furthermore, you should change admin.site.register(User, UserProfileAdmin) to admin.site.register(NetUser, UserProfileAdmin) so that your Admin backend is looking at your custom User model and not the standard Django User model.
You probably forgot to set AUTH_USER_MODEL in settings.py
AUTH_USER_MODEL = 'myapp.NetUser'
docs: substituting-a-custom-user-model
PS: However using some kind of profile imho is generally less painful )
Related
I have a models as
class Doctor(models.Model):
user = models.OneToOneField(
User,
on_delete=models.CASCADE,
primary_key=True,
related_name="user")
# other fields...
In my template, I easily can access the doctor object as request.user.doctor but using it in my views it causes the 'User' object has no attribute 'doctor' Error. so is it possible to access it as templates in my views too.
The related_name=… parameter [Django-doc] is the name of the relation in reverse, so to access the Doctor object from a User, since you have set this to user, you thus access the Doctor object with request.user.user, but that is misleading.
You thus better rename the relation to:
class Doctor(models.Model):
user = models.OneToOneField(
User,
on_delete=models.CASCADE,
primary_key=True,
related_name='doctor'
)
# other fields …
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.
I´m developing a website with Mezzanine and I´m having problem trying to configure modelTranslation plugin for it.I´m using Django 1.8.9 and Mezzanine 4.0.1, django_modeltranslation 0.11.
I have a model class GenericContent with some fields :
class GenericContent(models.Model):
image_footer = RichTextField(blank=True)
video_footer = RichTextField(blank=True)
summary = RichTextField(blank=True)
class BasicContent(Page, RichText, GenericContent):
pass
In the translation.py file for the basicContent app, we have the following modelTranslation translation definitions :
class TranslatedGenericContent(TranslationOptions):
fields = ('summary',
'image_footer',
'video_footer',)
class TranslatedBasicContent(TranslationOptions):
pass
translator.register(GenericContent, TranslatedGenericContent)
translator.register(BasicContent, TranslatedBasicContent)
With this configuration, it doesn´t show errors, but the basicContent doesn´t get translated(the inherited fields from genericContent are registered for translation, but in basicContent they don´t get translated, nor any other fields from the parent classes[Page and RichText], which are Mezzanine included classes and registered for translation).
If I try to modify the translation.py file :
class TranslatedGenericContent(TranslationOptions):
fields = ('summary',
'image_footer',
'video_footer',)
translator.register(GenericContent, TranslatedGenericContent)
translator.register(BasicContent, TranslatedGenericContent)
This other configuration gives and error when trying to run python manage.py sync_translation_fields
basicContent.BasicContent.image_footer_en: (models.E006) The field 'image_footer_en' clashes with the field 'image_footer_en' from model 'basicModels.genericcontent'.
basicContent.BasicContent.image_footer_es: (models.E006) The field 'image_footer_es' clashes with the field 'image_footer_es' from model 'basicModels.genericcontent'.
basicContent.BasicContent.summary_en: (models.E006) The field 'summary_en' clashes with the field 'summary_en' from model 'basicModels.genericcontent'.
basicContent.BasicContent.summary_es: (models.E006) The field 'summary_es' clashes with the field 'summary_es' from model 'basicModels.genericcontent'.
basicContent.BasicContent.video_footer_en: (models.E006) The field 'video_footer_en' clashes with the field 'video_footer_en' from model 'basicModels.genericcontent'.
basicContent.BasicContent.video_footer_es: (models.E006) The field 'video_footer_es' clashes with the field 'video_footer_es' from model 'basicModels.genericcontent'.
Have you ever experienced this problem? I´m looking into a fix for this. Any help would be appreciated!
I have a Profile model that has OneToOne relationship with User model.
I also have a Group model that has users field as ManyToMany to User.
I am trying to achieve a simple thing in Django Admin: being able to create a group with some new users and allowing to fill out some of the profile fields for those new users.
I tried a couple of things
class ProfileInline(admin.StackedInline):
model = Profile
class UserInline(admin.StackedInline):
model = Group.users.through
inlines = [ ProfileInline ]
class GroupAdmin(admin.ModelAdmin):
inlines = [
UserInline,
]
exclude = ('users',)
This is not working for me. I only see dropdown fields for user in my group admin and if I try to add a user the form does not have any profile fields.
You can't do that. Django admin inlines are only available if model has foreign key to other model. For you setup you have to find other solution related to your models relation.
I'm trying to add a custom field to already existed django's User model.
I want all fields of default User model(including password hashing functionality) + a custom field has_car, so I did ...
class MyUser(AbstractBaseUser):
has_car = models.BooleanField(default=False)
and register in admin panel admin.site.register(MyUser)
when I try to add open this model in admin panel I get this error.
OperationalError at /admin/myapp/myuser/
(1054, "Unknown column 'myapp_myuser.id' in 'field list'")
I'm not sure if its a mysqldb error or what?
I know I can use OneToOne or ForeignKey field but I simply want to extend User model.
again, It django==1.7b4 + Mysql
If you're just looking to add custom fields to the standard User model, your user model should inherit from AbstractUser instead of from AbstractBaseUser.
Don't forget to set:
AUTH_USER_MODEL = 'myapp.MyUser'
https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-django-s-default-user
I don't have errors with Django 1.5.4 (stable), but when I was testing my application on Django 1.6 beta 4 from official tar.gz I got error with validation models on 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 manage.py runserver console 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 need to do for solve this problem?
You must declare AUTH_USER_MODEL on your settings.py. In your case:
AUTH_USER_MODEL = 'your_app.ShopUser'
For me this was fixed in Django 1.6.5 by changing this:
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
pass
to this:
from django.contrib.auth.models import AbstractBaseUser
class CustomUser(AbstractBaseUser):
pass
No other changes needed in Django 1.6.5. #Stormlifter's suggestion has merit but I am using 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 CustomUser will be doing more than the default user and does need to be the AUTH_USER_MODEL='myapp.CustomUser' in settings.py as #jordiburgos suggested.
I hope this helps!
You want to inherit from AbstractBaseUser not from AbstractUser.
However, it looks like you just want to store some additional information for users, I wouldn't recommend using a custom user model for this. Just use relationships.
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 could make that a ForeignKey if you need Multiple ShopUser's to relate to a single user, but I think OneToOne makes the most sense.
As told by Django Documents:
Specifying a custom user model
AbstractBaseUser