Django Admin Custom Form Based on Request - python

I am trying to make custom form for a model in the django admin area with a select field that is created on submit based on the request.user. The user would then pick from the select and it would save specific settings to the model.
the model:
class Thing1(models.Model):
user = models.ForeignKey(User)
setting_1 = models.BooleanField(default=False)
setting_2 = models.BooleanField(default=False)
setting_3 = models.BooleanField(default=False)
But instead of having the user set the settings fields manually, I'd like to display a Select with something like:
- Default Settings
- Other Settings
And the user would select one and the system would save the settings booleans accordingly. The actual options of the select depends on the user, so I need request.user to be able to build that select field. I don't seem to have access to request in the ModelForm.
So I know that I can exclude the settings fields from the model in django admin, but how do I get the form to include the select with the correct things in the select options for the user and then have it save the correct settings in the model on save?
I've read a bunch of other questions about custom django admin fields and got some ideas, but don't have a clear picture.

Related

How to change in Django Admin the default form (dropdown list) for a foreignKey model?

I want to change in my django administration page the form of a field. In my model I have the 'user' as foreign key to another model. When I create a new 'programare' in the administration page the default form-field for user it's a dropdown with all entries of 'Cont' model. It's dizzy to search the right one, so I want to change that dropdown with a plain-text input or an autocomplete form.
class Programare(models.Model):
user = models.ForeignKey(to=Cont, on_delete=models.CASCADE, related_name='programari')
...
I found the answer for my question. For django ^2.0 admin
class ProgramareAdmin(admin.ModelAdmin):
model = Programare
autocomplete_fields = ['user']
admin.site.register(Programare, ProgramareAdmin)

How to alter label tag of fieldset in django admin page using admin.tabularinline

I customized my add user form in my django admin, using admin.tabularinline. So whenever the admin adds a user, information about Usermodel and UserProfile model is saved at the same time. However, the label tag of the UserProfile form says 'Staff Profiles'(with 's'). And it doesn't look good because there is only one profile for one user. Any ideas how to change the "Staff Profiles" into "Staff Profile"?
See the picture for reference:
As specified in docs, changing your verbose_name_plural in UserProfileInline
In admin declaration, add
class UserProfileInline(admin.YourInlineChoice):
verbose_name_plural="Staff Profile"
[Your params]

Django membership/dashboard plugin

I want to create a membership website where users can log in to a control panel separated from the admin panel.
Inside the control panel users can edit their settings, view statistics and use paid services.
What are the plugins I need to build this kind of websites?
How I can improve the security and automate the workflow ?
Start with the basic User module that comes with Django. When adding more settings to a user, you must create another model UserProfile that is linked to the base User model.
class UserProfile(models.Model):
user = models.OneToOneField(User) # The base User model takes username, password and email
# For the "paid services" you could use a boolean field and evaluate in your template
premium = models.BooleanField()
# Alternatively, a field that links to the services, which you'll have to include in your models.py
account_type = models.ManyToManyField(Service)
Make sure you have 'django.contrib.auth' and 'registration' in your INSTALLED_APPS in your project's settings.py, and work using those apps if they suit you.
For security, check https://docs.djangoproject.com/en/1.10/topics/security/

Storing new field to Django Admin "user" section from registration form

I have managed to add an additional field to the Registration form, "where did you hear about us?".
But I am not sure which files to edit in order to store the data from this field along with the users info.
i.e. When logging into the Admin section and go to "users" and view a users info I would like to see this field there.
Simplest way would be to store additional data in a UserProfile model about the user, e.g.
from django.contrib.auth.models import User
class UserProfile(models.Model):
# This field is required.
user = models.OneToOneField(User)
# Other fields here
where_heard_about_us = models.TextField()
You can then register the object as an inline object in your Django Admin

Writing user.get_profile() custom function which should create auth profile if there is not any

I am trying to write custom get_profile() function which should create user profile for users who are registered thru admin or any other way where post_save was not called.
How can I start this?
I guess that you have a model to handle user profile like this:
class UserProfile(models.Model):
"""Contains user profile fields not provided by User model"""
user = models.OneToOneField(User)
# Defined User profile fields like picture, phone, etc
So adding following line (maybe in your models.py after UserProfile model):
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
Allows access to the profile of a user (i.e. in templates: {% user.profile.phone %}) by creating it if not exists.
This is the way I solved in my site a problem like you describe.
Hope this helps
I am a bit confused. Are you trying to let users create account and sign in? Then use django-registration which is easy and works out of the box.

Categories