I have the following field in my Django Admin app:
class ContractForm(forms.ModelForm):
ativo = forms.CharField(
initial="this is a test"
)
The field value is visible when the logged user has all permissions (can add, change and view) on Admin's default auth console:
But when the user has only view permissions, the value is not shown:
Any thoughts?
Related
I have a Django model like this:
class Webhook(models.Model):
uuid = models.UUIDField(default=uuid.uuid4, editable=False)
url = models.CharField(max_length=256)
credentials = models.JSONField(null=True, blank=True, default=dict)
#admin.register(models.Webhook)
class WebhookAdmin(admin.ModelAdmin):
list_display = ("id", "uuid", "url")
fields = ("uuid", "url", "toggle_credentials_button", "credentials")
readonly_fields = ("uuid", "toggle_credentials_button")
def toggle_credentials_button(self, obj):
return format_html("""<a class="button">Toggle Credentials</a> """)
toggle_credentials_button.allow_tags = True
The Toggle Credentials button shown in the screenshot is a custom button. I want to show/hide credentials field based on the click/toggle of the Toggle Credentials button, i.e. if the credentials field is currently being shown, then hide the field after the button is clicked and vice-versa.
By default, the credentials field should always be hidden whenever a user visits the page shown in the screenshot, i.e. the showing of the credentials field shouldn't persist. If the user clicked the Toggle Credentials button to show the credentials field, then goes back to some other pages and again comes to this same page, then he should have to again click the button to view the credentials field.
Also please note that I won't be using this in any UI or Frontend, so I need the functionality to work in Django Admin only.
How can I achieve this requirement ?
Any kind of help would be appreciated. Thanks.
Also I am not much versed in Django, so if anyone finds any mistake in my code, please let me know.
when admin updates user profile how to access that obj in views and perform some task in django
i have user model and when user creates new account by default his id is deactivated but when admin activate their account from admin panel by change the user model field "is_active" to True
the i want to send that user an email and some other task also
so how to get that user's object
that activated by admin
Is there a way to send an e-mail to a user when their account is activated through the Django admin application? I can do it independently of the Django admin application but I want the admin to be signed in before they activate a user. Is there a way to achieve this without customizing the Django admin application code? Thanks in advance.
Well, you can override the ModelForm and add the email sending logic in clean method. Also use that modelform in the Admin class. For example:
class UserForm(forms.ModelForm):
manual_activation = forms.BooleanField() # a flag which determines if the user should be manually activated
class Meta:
model = User
fields = '__all__'
def clean(self):
manual_activation = self.cleaned_data.pop('manual_activation', False)
if manual_activation:
# send_email logics
return self.cleaned_data
class UserAdmin(admin.ModelAdmin):
form = UserForm
What will happen is that, in the User admin page, if you click on an user it will show an extra field in the form, named manual_activation. If you check and save the form, then in clean method, you can catch the value of manual_activation and based on that send email.
I have an existing model in which I want to add a User foreign key ex:
user = models.ForeignKey(User, unique=False)
If I set the parameter default=User.admin I get an attribute error.
If I try to set default=User.objects.get(username='admin') I get the error ValueError: Cannot serialize: <User: admin>
How can I set the default to be the User named admin or any other user?
Did you try setting primary key? The user table uses integers for the primary key.
I've followed https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#extending-the-existing-user-model to add a ManyToMany field for what games a user has played.
class Profile(models.Model):
""" Extended authentication profile storing specific info """
user = models.OneToOneField(User)
owned = models.ManyToManyField(OwnedStruct)
then add to admin by the following
class ProfileInline(admin.StackedInline):
""" Show profile inline with user """
model = Profile
verbose_name_plural = 'profile'
class UserProfileAdmin(UserAdmin):
""" Add inline to User """
inlines = (ProfileInline,)
...
admin.site.unregister(User)
admin.site.register(User, UserProfileAdmin)
In the database things look fine, but in the admin I see two fields representing the ManyToMany OwnedStruct. Before messing with the user, it shows the first as "Profile #1" and the second as "Profile #2". After selecting some options from Profile 1's M2M and clicking save, it appears to update that field correctly. If I update Profile#2, it does not save or work or appear to change anything. I'd expect it to only show one. What could cause two Profiles?
If I understand correctly the problem is that for some reason django admin doesn't care about OneToOneField and create more than one inline forms for the Profile. You can try to fix that with adding max_num = 1 to your ProfileInline class.
It must look something like:
class ProfileInline(admin.StackedInline):
""" Show profile inline with user """
model = Profile
max_num = 1
verbose_name_plural = 'profile'