I want to display the ModelA as a TabularInline of ModelB and at the same time want to maintain the history of changes to ModelA using the Django simple history module.
Is there a way to do this? Went through the official documentation of both TabularInline and Django simpleHistory but couldn't get much out of it.
Model A
class ModelAInline(admin.TabularInline):
model = ModelA
def has_add_permission(self, request, obj=None):
return False
Model B
class ModelBAdmin(admin.ModelAdmin):
list_display = ("name",)
search_fields = ("name",)
readonly_fields = ("last_changed",)
inlines = (ModelAInline,)
How you display in the admin doesn't factor in to whether or not the history of changes is maintained. You can register ModelA as another admin view that inherits from SimpleHistoryAdmin so that you can view history on it, but as long as ModelA is registered with django-simple-history, the history of the model will be tracked.
Related
Is it possible to load external data into a field for filling in?
Example: A field with for product names. However we already have the names of the products in another location, we just need to list these products within the field in the default django admin. Using resquets.
Thank you very much for your attention.
I think what you're looking for is how to customize the Django Admin, right? Check out this page in the documentation for a more detailed explanation, but here's an example that might help:
from django.contrib import admin
from .models import *
class ProductInline(admin.TabularInline):
model = Product
extra = 0
class OrderAdmin(admin.ModelAdmin):
inlines = [ProductInline]
admin.site.register(Order, OrderAdmin)
admin.site.register(Product)
This will show all of the products attached to a particular order when viewing that order from Django Admin.
You can prepopulate/fill a field in Django Admin with external data source. I guess you have some options defined somewhere outside your Django app and use those options as input for a charfield/integer field.
You can handle filling choices in a seperate Django form or overriding ModelAdmin methods. By creating a seperate form:
filter_choices = depends on your logic for loading external data
class AdminForm(forms.ModelForm):
filter_text = forms.ChoiceField(choices = filter_choices , label="Filter By",
widget=forms.Select(), required=True)
class Meta:
model = YourModel
#admin.register(YourModel)
class YourModelAdmin(admin.ModelAdmin):
form = AdminForm
You can try the 'formfield_for_foreignkey' method of the default ModelAdmin class
Example:
class MyModelAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "car":
kwargs["queryset"] = Car.objects.filter(owner=request.user)
return super().formfield_for_foreignkey(db_field, request, **kwargs)
This example (from the original docs) will populate the 'car' field with only specific values.
Pls note that this method suits a foreinKey. I'm not sure if it fits your requirements.
I'd like to create a formView(based on Django build-in CreateView class) to allow creating new user records, which are from two different models.
Both models sounds like one is user model and another is user profile model.
I wish one form with one submit button to approach it, but I found only one form_class could be assigned in one createView class in Django.
So I wonder is that possible to use CreateView to approach it? if not, any solution recommended? Appreciated if any assistance from you.
So you have two models for user and user_profile. One user one profile
so:
Try this:
#models.py
class User_profile(models.Model):
User= models.Foreignkey('User')
#add more user data
#forms.py
class UserProfileForm(ModelForm):
model = User_profile
fields = ['User']
#views.py
class SomeView(CreateView):
form_class = UserProfileForm()
Actually, I find the solution: use the User model and add profile's field in the same form as below
class userCreateForm:
email = forms.EmailField(label=_("E-mail"))
contact_number = forms.CharField(label=_("Contact"))
contact_address = forms.CharField(label=_("Address"))
class Meta:
model = User
fields = (UsernameField(), "email", "contact_number", "contact_address")
I have a user, admin and employee models, both employee and admin are related to the user model with a oneToOne field what i would like to do is in the admin form i have one field "user"
instead of having a drop down field with all users i would like to have a search box so that when i search for a specific user i find him and choose him to be an admin.
how?
i also tried to add user's fields to admin's form but i couldn't
i tried the inline thing, the parent_link in the admin's model... but i couldn't find a solution
Now i would like to minimise the search through the long users list by adding a search box to the field
I have solved this type of problem using django-ajax-selects package. Hope this will help you.
app/admin.py:
from ajax_select import register, LookupChannel
#register('users')
class UsersLookup(LookupChannel):
model = User
def get_query(self, q, request):
return self.model.objects.filter(username__icontains=q)
class EmployeeAdminForm(forms.ModelForm):
user = AutoCompleteSelectField('users', required=False,
help_text=None)
class Meta:
model = Employee
fields = '__all__'
settings.py
AJAX_LOOKUP_CHANNELS = {
'users' : {'model': 'auth.user', 'search_field': 'username'},
}
Use django's raw_id_fields. With that you can declare a foreign key object searchable.
I have a Wagtail project that uses django-fluent-comments and I would like to be able moderate the comments in the admin pages. Based on this tutorial I added this to my wagtail_hooks.py file:
class CommentAdmin(ModelAdmin):
model = FluentComment
menu_label = 'Comments'
menu_icon = 'list-ul'
menu_order = 200
add_to_settings_menu = False
list_display = ('user', 'comment')
modeladmin_register(CommentAdmin)
But when I go to the Admin pages, there is no comments tab and no errors shown. I have tried to add a Pages model to my Admin page and that worked fine.
The FluentComment model is just:
class FluentComment(Comment):
"""
Proxy model to make sure that a ``select_related()`` is performed on the ``user`` field.
"""
objects = FluentCommentManager()
class Meta:
proxy = True
(In this file) So I wonder if I have the correct model. But the if I add print model.objects.all() to my CommentAdmin class it shows all my comments in the log.
Change FluentComment to django_comments.models.Comment. FluentComment is Proxy model which wagtail's modeladmin does not like.
I'm only using forms.Form but I'm trying to show two choice fields that have selections of the associated models.
It basically needs to show the same names but in both fields. Here's what I'm using.
class ManagersForm(forms.Form):
class Meta:
model = A
leader = forms.ChoiceField()
co-leader = forms.ChoiceField()
Is there not just a way that I can parse the users?
users = MyUser.objects.filter(a=i)
You need to use a ModelForm not Form, if the field from the Model is a ForeignKey the form will render the field as a dropdown of the associated model:
class ManagersForm(forms.ModelForm):
class Meta:
model = A