How to add custom constraints to ForeignKey relationships in django - django

Say I wanted to add an extra attribute to the ForeignKey relationship in django to allow further customization.
Say for instance I want to add an attribute which when enabled will not allow repetitions ( I realize this particular use case already has a fix but it's just an example).
How would I approach such a thing?

It appears that the next version of django will contain the ability to define them on the model.
In the meantime, I think you'll need to create an empty migration and have it create the constraint manually via migrations.RunSQL

Related

Best approach to enrich User model in Django pluggable app

I am currently working on expanding https://github.com/suutari/drf-jwt-2fa, which provides two-factor authentication over jwt for rest-framework.
I would like to make the two-factor auth non-enforced; that is, users should be able to choose if they want this extra security.
For that purpose, I would like to add a boolean field (lets call it two_auth) to the user model.
What is the best approach to add this field?
I am currently thinking on some possibilities, but none of them seems to be neat enough:
Create a relation table (user_id, two_auth) without foreign-key enforcement: I should use signals for user deletion
Create a relation table (user_id, two_auth) with foreign-key enforcement: The foreign key should point to the model specified at settings.AUTH_USER_MODEL. I generally like model declaration parameters to be explicit, not patchable.
This is a nice guide on options to extend the built-in user model.

How to update primary key of an existing object in Django (with PostgreSQL)?

I need to change some objects primary keys in my django app, I wonder how can I achieve that?
Also my model has relations with other models.
Technically, it should be possible with an update query on the queryset:
MyModel.objects.filter(id=old_id).update(id=new_id)
The relations should cascade too if the constraints in the database have been set up correctly, but in general, I'd avoid updating PKs.

How do we change Field Names in our existing Users table in django

I am trying to Edit Users Table in Django. I am using Users Table to login or register a users. I have to add a new field name Role in that Table but i can't find any option to edit that existing table in admin section.
i just try to field some files to field out where the code of that existing Table is but did't get it.
is there any way to Edit the Table or I have to Create a New Table and have to create a new method of registration.
i am not expert so it's hard to me understand things.
Well first, the Django admin interface it's just for performing CRUD operations over already existing models, you are not able to change in any way the database tables (at lest not using the "out of the box features") using the admin interface.
Said that in order to do what you want to do, with any model (not just User), you should:
Add the field to the model.
Instruct the admin interface to list this fields along the others.
Now the user model is kind of a special model here so I'll recommend a couple of readings you should complete before go forward with the model User customization.
References (User customization): Substituting a custom User model, Extending the User model.
And for the admin interface ...
Reference (admin interface): ModelAdmin options, special attention here to list_display

Django User model is good enough to be used in production

Is it good to use django custom user model if you want to create a website for production purposes with some tweaks using AbstractBase
I think so, if you don't need modify it later.
However, you can extending the User model, even you don't use any custom field at the moment. In this way, you will be able to add any field in the future.

Django - DEV django bulk update/add m2m field ... how to exactly?

I need to bulk add m2m fields and looks like django dev version supports bulk.
But they only give example of how to add a single object? I cant find any examples of it being done in bulk.
Also is this method more effective then solution proposed here?
http://grokbase.com/t/gg/django-users/158c8q3ty3/bulk-add-m2m-relationship-for-multiple-instances (