Customize foreign key dropdown in Django Admin Site - python

I'm having trouble finding the best way to override and add custom html to an edit/add model form in my Django admin site.
Here are the two models involved here:
Icon model used to store "Font Awesome" Icons:
class Icon(models.Model):
name = models.CharField(max_length=100, null=False)
style = models.CharField(max_length=10, choices=STYLE_CHOICES, null=False)
retired = models.BooleanField(default=False)
def delete(self):
self.retired = True
self.save()
objects = NotRetiredManager()
objects_deleted = DeletedManager()
def __str__(self):
return self.name
Workbook model that holds foreign key reference to the above Icon model:
class Workbook(models.Model):
client = models.ForeignKey(Client, on_delete=models.SET_NULL, null=True)
icon = models.ForeignKey(Icon, on_delete=models.SET_NULL, null=True, blank=True)
name = models.CharField(max_length=100)
workbookLink = models.CharField(max_length=1000)
retired = models.BooleanField(default=False)
def delete(self):
self.retired = True
self.save()
objects = NotRetiredManager()
objects_deleted = DeletedManager()
def __str__(self):
return self.name
Here are the overridden admin models for the above models:
class BaseAdmin(AdminImageMixin, admin.ModelAdmin):
def delete_queryset(self, request, queryset):
for obj in queryset:
obj.delete()
#admin.register(Workbook)
class WorkbookAdmin(BaseAdmin):
list_display = ("name", "client")
list_filter = (NameFilter, ClientNameFilter)
ordering = ("name", )
#admin.register(Icon)
class IconAdmin(BaseAdmin):
fields = ("name", "style", "icon_display")
list_display = ("icon_display", "name", "style" )
list_display_links = ("icon_display", "name")
list_filter = (NameFilter, )
ordering = ("name", )
def icon_display(self, obj):
return mark_safe(f'<i class="{obj.style}{obj.name}"></i>')
readonly_fields = ["icon_display"]
Here is a list display of some Icons I have in my database:
Currently, the add/edit page for a Workbook on my Admin Site looks like this:
I would like for that dropdown in that second screenshot to be customized similar to the "Icon Display" column in that first screenshot so that a user would choose from graphical list of icons as opposed to the default choicefield form containing the Icon names.
I've looked into the Django docs as well as similar questions on here such as this Similar Stack Overflow Question; however, I'm not fully understanding the proper way to implement something like this.
I hope the information I provided about my app is useful. Please let me know if you'd like me to provide any additional information, or add any clarifications!

Related

Django , how to show 'secondary property' of parent in TabularInline

I try to do tabular inline admin.
In the child tab, if we include a ForeignKey field, it will show the str property of that foreign model.
But how to also show another property of that foreign model?
Here is my models.py
class RawMaterial(models.Model):
name = models.CharField(max_length=15)
ubuy = models.CharField(max_length=5)
usell = models.CharField(max_length=5)
uconv = models.DecimalField(max_digits = 5,decimal_places=2)
def __str__(self):
return self.name
class Coctail(models.Model):
name = models.CharField(max_length=20)
def __str__(self):
return self.name
class Ingredient(models.Model):
coctail = models.ForeignKey(Coctail,
related_name='ingredient',
on_delete=models.CASCADE)
rawmaterial = models.ForeignKey(RawMaterial,
related_name='ingredient',
on_delete=models.CASCADE)
qty = models.DecimalField(max_digits = 5,decimal_places=2)
def __str__(self):
return self.rawmaterial
def rawusell(self):
return self.rawmaterial.usell
rawusell.short_description = 'UOM'
Here is my admin.py
from django.contrib import admin
# Register your models here.
from .models import *
admin.site.register(RawMaterial)
class IngredientInline(admin.TabularInline):
model = Ingredient
list_display = ('rawmaterial', 'qty', 'rawusell')
class CoctailAdmin(admin.ModelAdmin):
inlines = [IngredientInline]
admin.site.register(Coctail, CoctailAdmin)
and here what I got
My question is : How to show rawmaterial.usell in Ingredient tab?
Sincerely
-bino-
You can show the rawmaterial.usell field in the ingredient tab but it will not be editable. Since, any field can only be editable if they are a field of that model (Without using any custom form and logic).
So, if you want rawmaterial.usell to be editable, you will have to make a rawmaterial admin
You can show the rawmaterial.usell in IngredientInline by doing this.
class IngredientInline(admin.TabularInline):
model = Ingredient
readonly_fields = ('rawusell', )
list_display = ('rawmaterial', 'qty', 'rawusell')
def rawusell(self, obj):
return obj.rawmaterial.usell
This will start showing usell in the inline admin.

Stuck on linking ManytoMany relationships with Modelform

I'm pretty new to Django and I am working on a project that currently requires the following:
I have two basic structures: a Project model and a TeamMember model- both related to each other through a ManytoMany relationship. Then I have an TMAssigned 'through' class. The team member will have many projects assigned to it over time.
I have a ModelFrom which creates a Project model through the creation of the form.
My question is, How do I link the team member to the newly created project upon the submission of the form?
Here is a bit of my model & form code:
TeamMember
class TeamMember(models.Model):
firstname = models.CharField(max_length=100, default= "First Name")
lastname = models.CharField(max_length=100, default= "Last Name")
fullname = models.CharField(max_length=100, default= "Full Name")
email = models.EmailField(max_length=254)
cellphone = PhoneNumberField(null=False, blank=False, unique=True)
numberofcases = models.IntegerField(max_length=10000, default=0)
#property
def fullnamefunc(self):
fullname = "{} {}".format(self.firstname, self.lastname)
return fullname
def __str__(self):
return self.fullname
Project
class Project(models.Model):
pursuitname = models.CharField(max_length=500)
datecreated = models.DateTimeField(auto_now=True)
bdmember = models.ManyToManyField('team.TeamMember')
Views.py
class bdFormView(TemplateView):
template_name = os.path.join(BASE_DIR, "templates/masterform/bdform.html")
def get(self,request):
form = bdForm()
return render (request, self.template_name, {'form': form})
def post(self, request):
form = bdForm(request.POST)
if form.is_valid():
print("form is valid")
project = form.save(commit=False)
project.save()
text = form.cleaned_data['briefcard']
Form.py
class bdForm(forms.ModelForm):
bdmemberlist = TeamMember.objects.all().order_by('lastname')
pursuitname = forms.CharField()
bdmember = forms.ModelChoiceField(queryset= bdmemberlist)
addbdteam = forms.ModelMultipleChoiceField(
queryset=TeamMember.objects.all().order_by('lastname'), widget=Select2MultipleWidget, required=False)
class Meta:
model = Project
fields = ['pursuitname','addbdteam','bdmember',]
def __init__(self, *args, **kwargs):
if kwargs.get('instance'):
initial = kwargs.setdefault('initial', {})
initial['projects'] = [t.pk for t in
kwargs['instance'].project_set.all()]
forms.ModelForm.__init__(self, *args, **kwargs)
def save(self, commit=True):
instance = forms.ModelForm.save(self, False)
old_save_m2m = self.save_m2m
def save_m2m():
old_save_m2m()
for project in self.cleaned_data['bdmember']:
instance.teammember_set.add(project)
Thanks in advance!!
Edit- after doing some more research, I've removed the "Through" model from the script and am trying to rely on the form.py save method to do the join. However, when I do this- the two are still not linking up properly.
Since only your admin (superusers?) will log in, you can start off by using the in-built Django Admin.
I would recommend this for you, at least for now, because you're a beginner and the Admin Form is stunningly simple to use. Then, you can create a custom form later on when you're more comfortable. :-)
With this in mind, you can try eliminating the 'through' table (you may need to reset your migrations), and try this.
Admin.py
from django.contrib import admin
from .models import TeamMember, TMAssigned, Project,
TeamMembersInLine(admin.TabularInline):
model = TeamMember
extra = 1
#admin.register(Project):
class ProjectAdmin(admin.ModelAdmin):
list_display = ('pursuitname', 'bdmember ', 'datecreated')
inlines = [TeamMembersInLine]
Here's another answer that delves into the through table. It was asked by someone in your situation and the answer is relevant too.

Django - 'extra_fields' in CustomModelForm is giving 'Unable to lookuup' error in models inline interface when trying to load that form

I've one app named Question where i defined two models Question and Alternative in models.py as follows :
class Question(models.Model):
question = models.CharField(max_length=255, blank=False, null=False)
chapter = models.ForeignKey(Chapter, on_delete=models.CASCADE)
rating = models.IntegerField(default=1)
class Alternative(models.Model):
alternative = models.CharField(max_length=255, blank=False, null=False)
question = models.ForeignKey(Question, on_delete=models.CASCADE)
i've made a Custom form AlternativeForm where i've created a extra field which i want to appear in my Alternative forms as well as Question admin view where Alternative fields will appear in the inline view But the extra field value will not be saved in DB(cause i want to do some manual operations with the value of that fields). my forms.py is as follows:
class AlternativeForm(forms.ModelForm):
extra_field = forms.BooleanField(required=False)
def save(self, commit=True):
extra_field = self.cleaned_data.get('extra_field', None)
# will do something with extra_field here...
return super(AlternativeForm, self).save(commit=commit)
class Meta:
model = Alternative
fields = '__all__'
and in my admin.py i've made an inline relationship between them as follows:
class AlternativeInline(admin.TabularInline):
form = AlternativeForm
model = Alternative
#admin.register(Question)
class QuestionAdmin(admin.ModelAdmin):
inlines = [AlternativeInline,]
#admin.register(Alternative)
class AlternativeAdmin(admin.ModelAdmin):
model = Alternative
form = AlternativeForm
I'm getting AttributeError: Unable to lookup 'extra_field' on Alternative or AlternativeInline in this case. I want to show those extra field in the Inline view of Questionapps admin view. Is there any way to do it or what is wrong in my current approach.
Thanks.
I found the solution when speculating this post. One should define the label field in the custom field like the following to avoid such error AttributeError: Unable to lookup 'extra_field' on Alternative or AlternativeInline.
class AlternativeForm(forms.ModelForm):
extra_field = forms.BooleanField(label='is_answer', required=False)
def save(self, commit=True):
# extra_field = self.cleaned_data.get('extra_field', None)
# ...do something with extra_field here...
return super(AlternativeForm, self).save(commit=commit)
To have the extra fields in the admin:
class YourModelAdmin(admin.ModelAdmin):
form = YourModelForm
fieldsets = (
(None, {
'fields': ('other fields here', 'extra_field',),
}),
)

Can't edit but can add new inline in Django admin

Here are my models
class Note():
note = models.TextField(null=False, blank=False, editable=True)
user = models.ForeignKey(to=User, null=True, blank=True)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey("content_type", "object_id")
And an inline I created this model to incorporate in any admin is below
class NoteInline(GenericTabularInline):
model = Note
extra = 0
What I need here is, I want to see all the current notes but don't want the logged in user to edit them. At the moment user can edit old and add new. So here is what I did,
class NoteInline(GenericTabularInline):
model = Note
extra = 0
def get_readonly_fields(self, request, obj=None):
if obj and 'change' in request.resolver_match.url_name:
return ['note', 'user', ]
else:
return []
But now if user adds new note he sees a disabled (not editable) note text ares. However user can see old fields not editable.
How to implement this functionality?
I am having the same inquiry.
However, I do not care if the fields in the inline are "read only" or not. I just do not want them changed once they are created.
For this purpose, I created a NoteForm in forms.py which raises a validation error if the instance has changed while it has initial data:
class NoteForm(forms.ModelForm):
def clean(self):
if self.has_changed() and self.initial:
raise ValidationError(
'You cannot change this inline',
code='Forbidden'
)
return super().clean()
class Meta(object):
model = Note
fields='__all__'
admin.py:
class NoteInline(GenericTabularInline):
model = Note
extra = 0
form = NoteForm

Django admin many to many subset

I'm trying to integrate in Django admin the next three related models:
# models.py
class Seminar(models.Model):
title = models.CharField(max_length=128, unique=True)
start_date = models.DateField(db_index=True)
end_date = models.DateField(db_index=True)
class Event(models.Model):
title = models.CharField(max_length=128)
start_date = models.DateTimeField(db_index=True)
end_date = models.DateTimeField(db_index=True)
seminar = models.ForeignKey('Seminar')
class Registration(models.Model):
name = models.CharField(max_length=128)
first_name = models.CharField(max_length=128)
seminar = models.ForeignKey('Seminar')
events = models.ManyToManyField('Event', null=True)
# admin.py
class EventInline(admin.TabularInline):
model = Event
class SeminarAdmin(admin.ModelAdmin):
list_display = ('title', 'start_date', 'end_date')
inlines = [
EventInline,
]
class RegistrationAdmin(admin.ModelAdmin):
list_display = ('seminar', 'name', 'first_name')
As you can see, each seminar may have several events, added from the Seminar admin as inline entries.
My problem is with registrations, since they are:
related to a seminar
can "subscribe" to several events of this seminar
Of course, the admin lists all events and not the subset related to the seminar, so:
is it possible to achieve this from the admin (with as low tweaks as possible)?
is the Registration M2M on Event appropriate or should I relate this two models in a different way?
Thanks!
Well so, just before asking here, I did something similar to Django's auth.User model by using different forms for add/change views. I hoped there was a better solution, obviously not.
So here is what I did:
# models.py
class Registration(models.Model):
# [...] - add form excludes it, should allow blank on events field
events = models.ManyToManyField('Event', blank=True, null=True)
# admin.py
class RegistrationAdmin(admin.ModelAdmin):
change_form = RegistrationChangeForm
def get_form(self, request, obj=None, **kwargs):
defaults = {}
if obj is None:
defaults.update(exclude=('events',))
else:
defaults.update(form=self.change_form)
defaults.update(kwargs)
return super(RegistrationAdmin, self).get_form(request, obj, **defaults)
# forms.py
class RegistrationChangeForm(forms.ModelForm):
class Meta:
model = Registration
exclude = ('seminar',)
def __init__(self, *args, **kwargs):
super(RegistrationChangeForm, self).__init__(*args, **kwargs)
self.fields['events'].choices = Event.objects.filter(seminar=kwargs.get('instance').seminar).values_list('pk','title')
So, when adding we simply ignore events (registration is saved with nulled events) and then, on change a list of events can be selected based on the previously defined seminar which is then ignored (since we can't reload the related events list).

Categories

Resources