I have a UUID field in Django 1.6 imported from django-uuidfield v0.4.0. I can't change Django versions.
id = uuidfield.UIIDField()
And all is fine, except when attempting to view the model in the Admin, when this error is displayed:
expected a character buffer object
The problem seems to be this line:
return mark_safe(force_text(value).translate(_js_escapes))
from site-packages/django/utils/functional.py
I have tried to exclude this field from the Admin view by excluding it specifically
exclude=('id',)
and by including a different field explicitly, hoping it would only process that field and not the ID field (as the docs seem to indicate).
include = ('email',)
But the error persists. It seems as if the Admin site is examining the fields anyway, ignoring any settings in the Admin setup. I have also set 'editable=False' in the model definition with no effect for the id field.
I don't need to manipulate or edit this field in any way from the admin screen, just hide it so it does not crash the admin.
I'm not sure where you are importing that from, but Django has included its own UUIDField since version 1.7. You should certainly be using that instead.
Related
I'm starting a project using an existing db. The models generated from inspectdb have
(...null=True, blank=True)
in various fields. The documentation mentions that the blank argument is related to django forms. I'm making a web service project, using django rest framework and the django ORM. Absolutely no view component (autogenerated forms, html templates, etc) of the MVC.
Does that mean it's safe to remove blank=True from my models?
Without blank=True your application will fail validation when trying to serialize data with blank fields. Django rest framework ModelSerializer enforces any kind of validation that is on the model fields. It works similarly to Django ModelForms.
If you're not using ModelSerializers and try to save a blank value against a field without blank=True your save method will throw an exception instead.
TL;DR: if you remove blank=True your DRF serializer will fail validation against empty fields.
Take a look at this question on how validation works in DRF.
Hope this helps!
Probably not.
Whatever your service is, you probably want to validate data. And Django's validation relies on that blank=True. It's not just for forms and templates. From the documentation:
blank is validation-related.
Django Rest Framework will use the blank attribute on a model field in serializer validation. When blank is False, which is the default value, the ModelSerializer will raise a ValidationError if a value is not set for this field in a POST request.
Basically, if you remove blank=True from a field and try to create a record with no value for that field, you will get a 400 response from your server.
Nothing will break, just this request will fail to create a resource. In my book I'd call that safe. It really just comes down to whether you want the field to be required or not. But since you have null=True, it seems reasonable to have blank=True.
I have an existing django 1.9 application on python 3.5 and I'm trying to bring django cms into it. Right now, I'm following the manual install instructions here: http://docs.django-cms.org/en/release-3.3.x/how_to/install.html
I want to use the existing postgres db for storing cms data. Right now, all I've done is add the cms apps to INSTALLED_APPS as well as the middleware and the templates, all in the settings file. When I launch my app I get the FieldError:
django.core.exceptions.FieldError: Local field 'created_by' in class 'PageUser' clashes with field of similar name from base class 'User'
I ran through this http://docs.django-cms.org/en/develop/reference/configuration.html#custom-user-requirements and made sure our custom User was inheriting properly and had the right fields and methods.
Our custom User model does inherit from a custom mixin that adds a created_by to the user model.
I can't find a workaround that solves this. Any suggestions would be greatly appreciated.
You should rename the field. Something like this
class PageUser(User):
page_user_created_by = models.ForeignKey(settings.AUTH_USER_MODEL,
related_name="created_users", db_column='created_by')
You can read more about it here
In Odoo Opportunity Report I would like to add field customer from res.partner.
I created addon (which installed, and does other things besides, so I am sure that addon works) in which I have inherited from https://github.com/odoo/odoo/blob/10.0/addons/crm/report/crm_opportunity_report.py .
And added a field
customer = fields.Boolean('Customer', related='partner_id.customer', readonly=True)
But field Customer doesn't appear in report when I click '+' in Reports->Pipeline.
What did I miss?
It is not enough to define a field. Odoo reporting is working on database views. So by adding a new field, you have to change the view, too. Normally or in newer versions Odoo has good extendable view definitions by using the init(). In your example it's the old "bad to extend" view definition, so you have to override the whole init.
In my django app I have a custom user model with a username field called my_username. Recently I made this field non unique (actually it is now unique_together with another field). This makes django to emit the following warning in manage.py:
(auth.W004) 'MyUser.my_username' is named as the 'USERNAME_FIELD', but it is not unique.
HINT: Ensure that your authentication backend(s) can handle non-unique usernames.
Is there a way to prevent this warning for displaying? I only found ways to disable all warnings, but I want to disable only that specific one.
Since Django 1.7, there is a setting to silence certain warnings. If you are using Django 1.7 or later, you can add the error code to the SILENCED_SYSTEM_CHECKS setting:
# settings.py
SILENCED_SYSTEM_CHECKS = ["auth.W004"]
source: https://docs.djangoproject.com/en/dev/ref/settings/#silenced-system-checks
With Django 1.5 and the introduction of custom user models the AUTH_PROFILE_MODULE became deprecated. In my existing Django application I use the User model and I also have a Profile model with a foreign key to the User and store other stuff about the user in the profile. Currently using AUTH_PROFILE_MODULE and this is set to 'app.profile'.
So obviously, my code tends to do lots of user.get_profile() and this now needs to go away.
Now, I could create a new custom user model (by just having my profile model extend User) but then in all other places where I currently have a foreign key to a user will need to be changed also... so this would be a large migration in my live service.
Is there any way - and with no model migration - and only by creating/overriding the get_profile() function with something like my_user.userprofile_set.all()[0]) somewhere?
Anyone out there that has gone down this path and can share ideas or experiences?
If I where to do this service again now - would obviously not go this way but with a semi-large live production system I am open for short-cuts :-)
Using a profile model with a relation to the built-in User is still a totally legitimate construct for storing additional user information (and recommended in many cases). The AUTH_PROFILE_MODULE and get_profile() stuff that is now deprecated just ended up being unnecessary, given that built-in Django 1-to-1 syntax works cleanly and elegantly here.
The transition from the old usage is actually easy if you're already using a OneToOneField to User on your profile model, which is how the profile module was recommended to be set up before get_profile was deprecated.
class UserProfile(models.Model):
user = OneToOneField(User, related_name="profile")
# add profile fields here, e.g.,
nickname = CharField(...)
# usage: no get_profile() needed. Just standard 1-to-1 reverse syntax!
nickname = request.user.profile.nickname
See here if you're not familiar with the syntactic magic for OneToOneField's that makes this possible. It ends up being a simple search and replace of get_profile() for profile or whatever your related_name is (auto related name in the above case would be user_profile). Standard django reverse 1-1 syntax is actually nicer than get_profile()!
Change a ForeignKey to a OneToOneField
However, I realize this doesn't answer your question entirely. You indicate that you used a ForeignKey to User in your profile module rather than a OneToOne, which is fine, but the syntax isn't as simple if you leave it as a ForeignKey, as you note in your follow up comment.
Assuming you were using your ForeignKey in practice as an unique foreign key (essentially a 1-to-1), given that in the DB a OneToOneField is just a ForeignKey field with a unique=True constraint, you should be able to change the ForeignKey field to a OneToOneField in your code without actually having to make a significant database migration or incurring any data loss.
Dealing with South migration
If you're using South for migrations, the code change from the previous section may confuse South into deleting the old field and creating a new one if you do a schemamigration --auto, so you may need to manually edit the migration to do things right. One approach would be to create the schemamigration and then blank out the forwards and backwards methods so it doesn't actually try to do anything, but so it still freezes the model properly as a OneToOneField going forward. Then, if you want to do things perfectly, you should add the unique constraint to the corresponding database foreign key column as well. You can either do this manually with SQL, or via South (by either editing the migration methods manually, or by setting unique=True on the ForeignKey and creating a first South migration before you switch it to a OneToOneField and do a second migration and blank out the forwards/backwards methods).