How can I set Django model options in runtime? - django

I'm working on an admin panel and want to provide some configurable options for the end user.
For example, when I have a User model I want an option in my panel to be able to select which fields are required.
How can I change the model options in runtime and make it persistent?

Related

Is there a way to manage admin panel edit permissions by model property in Django?

So, I want to be able to control write permissions for a model in Django admin panel using model property value.
Here's an example: we have a model called Organization with property is_production, which is boolean. Also, we have two roles: Developers and Customer Support. I want users from Developers group to be able to edit Organizations with is_production = False, and users from Customer Support to be able to edit ones with is_production = True. Is there a no-code / low-code way to do it, like an extension / Django battery?
Thanks for your time!

In Django Admin, can I display a listing of an unrelated record set alongside an admin form?

My use case is to implement something like a messaging form, allowing an administrator to write a message, then send it to group that they will filter from a list of users, from the User model. This is similar to the messaging to usergroups functionality in Joomla! so it's not too weird a use case.
So my admin page for the "Message" model would need to contain the Message creation form and a second recordset of site Users, which could be filtered down to those who the administrator wishes to contact.
Is this kind of thing possible in Django Admin, or do I need to dip into heavily customising an admin page?

django staff users manage their own users only

In my Django app a user can register to the site and receive staff_user privileges from the admin.
After that the staff user can create and manage some other users (normal users) using default django admin site.
Now, I would like to let the staff user see and manage only the users he created from the admin site, I don't want him to see other users created by another staff user.
how can I do that? I imagine I need to modify admin.py right?
Don't modify the admin site.
In general, you have the following tools available:
Create groups
Add users to groups
Create custom permissions on your models, to indicate certain actions
https://docs.djangoproject.com/en/1.4/topics/auth/#custom-permissions
However, what you are asking: Now, I would like to let the staff user see and manage only the users he created from the admin site is not possible in django-admin.
The Django-admin site is only intended as a glorified development tool for fully trusted users, not as a customizable app for end users.
If your project requires an admin site with any of the following ...
Customized administraion functionality.
Exposure to any user that is not completely trusted.
... then I'm afraid you have to create your own custom app.
You can replace the stock UserAdmin with your own which overrides queryset() and does the filtering. The bigger issue is what to filter by. The default User model does not store a "created_by" in the model instance. So you would need to add this information whenever a User is added.
How best to do this depends on your Django version.
Django 1.5 introduced a "Configurable User model" which makes this very easy.
https://docs.djangoproject.com/en/dev/releases/1.5/#configurable-user-model
In earlier versions you would either have to monkeypatch the User model, or store that information in a separate "user profile" attached 1:1 to the User.
https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model
Regarding the trusting of users (which wasn't a topic but I feel the need to comment on thnee's answer) in the Django admin, check out the links in my answer here: Should I use Django's Admin feature?

Adding extra functionality to Django Admin

It's a fairly simple idea, but I'm not sure where I should start reading.
All I want to do is add an extra action to a model in the Django Admin.
For example, I have a basic client management system, in that I have models for Clients, Companies, and Invoices to keep track of who owes what. All I want to do is add an action or button that when used, pulls information from the Invoices, Clients, and possibly Companies and then puts that information into a PDF document.
Any suggestions?
You'll have to override your admin templates for each of your models to add a button => Overriding Admin Templates
To generate a PDF define a view that is called once your button is pressed. Make sure to set the right permissions for your admin users.

Extending Sitecore User Profile - Which Field Types Can I use?

I have created a custom user profile in Sitecore 6.4.1, and am trying to add a basic droplist to the users profile. It seems that no matter which type of field I add to the user profile, it renders out in the Edit User dialog as a plain text field. Is there a restriction to the user profile, or some other method I need to use to allow content editors to pick an item to associate with a user profile?
The user manager cannot properly render all fields types. You should stick to single line text values if possible and avoid the user manager interface to edit the info itself. Use it more as a custom data store.
Edit: for reference, I've used this feature and extended profiles with simple fields for a login-based system with Sitecore. Our site allows users to register an account, which creates extranet users with an "extended profile" in the Core DB. When these users access thier profile page on the front-end site when they're logged into the front-end, they can edit basic data, like name, address, etc. I store these basic data points in the custom profile SLT fields, so its used merely as a data store. No one actually edits the content from the Sitecore shell's user manager.