Django admin - how to store references to models in model field? - django

I'm making some big changes in Django Admin and need to make an application to group various models to groups and then show it in menu.
I have a function to generate list of all available admin models including permissions.
So I make 1 model to add group (id, name). Via Inlines I want to add specific models to groups.
What is the best way to store models references? Should I store it as string and during my menu is generated I should parse the name, find specific model and generate its url in admin?
Thanks for clues.

Sounds like contenttypes would help you.

Related

How can I implement authentication in Django

I am new to Django.
I am going to build simple register and login fullstack application by using React and Django.
My problem is when I received register request with form data.
Is it ok to create custom table for users?
I am going to create another table related to user table.
So in that case, there must be id in the users.
That's why I am going to create custom table.
Please help me it is good practice.
You can abstract from AbstractBaseUser and then you can customise the user model and to specify it in your settings file.
Please see the django documentation here:
https://docs.djangoproject.com/en/4.1/topics/auth/customizing/#substituting-a-custom-user-model
In Django you can link between multiple table by different relationships depends on what you want like:
OneToOne
ForeignKey
ManyToMany
And by default when you create a model django create a pk field it is ID for table, you can make another field as a primary key for model
When you use one of those relationships django by default use model id to link between them
and you can also create a custom user model to use it
Good luck

How to properly import CSV datasets to Django models with ManyToMany / ForeignKey relationships?

Novice alert! I want to use django-csv-importer 0.1.3.5 to import data to models with ManyToMany relationships.
Should make a different model for every relation or should I put everything in the same model?
Also, note that I want to allow registered users on my site to submit a form to that same model, would that still work?
I use django import export which is easy to use and supports many file types.
It comes with admin integration. I haven't tried many too many but tried with a foreign key, it works pretty well. Yes, the user can submit the form as well.
See https://django-import-export.readthedocs.io/en/latest

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

how to create Groups programatically in Django

I am setting up a member site using django, in which members belong to groups that they create and then invite other users to join.
It seems that what I am looking for is to use the django groups functionality, but the documentation on how to go about this is minimal at best - at least I haven't found any. It basically talks about creating groups in the Admin console, which is not what I am trying to do. I would like to do it programatically/dynamically.
Another way to go about this would be to at a foreignkey to the User model up to a group model, however I can't add a foreignkey to the generic User model.
I have read lots of stuff that google threw up at me from my searches. none helpful.
How would I go about this?
Thanks
Well a Group is just another model in Django (one of the models defined in the Django library).
You can thus create a group with:
from django.contrib.auth.models import Group
g1 = Group.objects.create(name='Name of the group')
The Group model has two many-to-many relations: one to Users (with related name users), and one to Permission (with related name permissions). So we can use the corresponding managers to add and remove Users and Permissions.
Then you can for example populate the group with users like:
g1.user_set.add(user1, user2, user5, user7)
You can also add permissions to a group with:
g1.permissions.add(perm1, perm3, perm4)

Creating Views like Django Admin Panel

is there any way to create views like the ones shown on Admin Panel without any effort, I mean, generic views that display all the fields and have the ability to add foreign key items and such, just like admin panel?
I've tried using
django.views.generic.edit.FormView
django.views.generic.edit.CreateView
django.views.generic.edit.UpdateView
django.views.generic.edit.DeleteView
But I am forced to specify fields, and I have quite a lot. It also doesn't allow me to create objects for the foreignkey fields if there's any.
Thanks in advance.