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!
Related
I am using Python 3.10.0 and Django 3.2.9
I created a new app in my django project, and then created a model called Posts. Everything works as intended except for how it's name is being displayed in the admin section of the website. It says "Postss" (double s in the end).
If I change the model name to "Post" or just one letter "P" for instance, django automatically adds "s" to the end of it's name, so it is being displayed as "Posts" or "Ps" then.
Other words, whatever the model name is, django automatically adds "s" in the end of it's name in the admin pannel of the website.
Is it possible to tell django I don't want that "s" to be added? How?
Thanks a mil, guys.
Normally models have singular names, so Post, not Posts. You thus might want to consider renaming your model.
If you still want to use Posts as model name, you can specify the verbose name of the model, and the plural verbose name with the verbose_name [Django-doc] and the verbose_name_plural model options [Django-doc] respectively:
class Posts(models.Model):
# …
class Meta:
verbose_name = 'post'
verbose_name_plural = 'posts'
But as said before, if you rename your model Post, Django will convert the PerlCase to a name with spaces and as plural append the name with an s.
I have two models with one-many relationship (Organization hasMany Locations)
class Organizations(models.Model):
name = models.CharField(max_length=40)
class Location(models.Model):
name = models.CharField(max_length=50)
organization = models.ForeignKey(Organizations, to_field="name", db_column="organization_name", related_name='locations')
class Meta:
db_table = u'locations'
Now, while trying to pre-fetch locations while retrieving "Organizations" I am getting this error.
Organizations.objects.prefetch_related('locations')
AttributeError: Cannot find 'locations' on Organizations object, 'locations' is an invalid parameter to prefetch_related()
However, if I preload the other way around it works fine.
Location.objects.prefetch_related('organization')
Note: After pre-loading organizations from location model, the previous error does not occur anymore.
I am using Django version: 1.8.6
It seems this error is only present in the django shell. This works fine in the django app itself
I just installed django-cities-light for my project using DRF and I can't get it work.
My User Model define a city and coutry field as foreignkey, and this is what i get when i tried to get my users :
Could not resolve URL for hyperlinked relationship using view name "city-detail".
You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field.
Any ideas ?
Thanks !
Using django-cities-light with Rest Framework 3, you need to use their provided view_name:
cities-light-api-{model}-detail
class FooSerializer(HyperlinkedModelSerializer):
url = relations.HyperlinkedIdentityField(view_name="foo-detail")
city = relations.HyperlinkedRelatedField(
view_name="cities-light-api-city-detail", queryset=City.objects.all(),
)
class Meta:
model = Foo
read_only_fields = ('id',)
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 )
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