I give you my example right now because it's too hard to explain it, well, I don't know XD. I have an object named Step like this :
class Step(models.Model):
# Intitulé de la question
title = models.CharField(max_length=300)
# Appartenance
belonging = models.ForeignKey('Survey', blank=True, null=True)
# Renvoi à lui-même pour arborescence
parent = models.ForeignKey('self', blank=True, null=True)
# Ordre de la réponse
order = models.PositiveIntegerField(default=0)
# Complément d'information
add_info = models.TextField(blank=True, verbose_name="Additional informations")
def __str__(self):
return (self.title)
def children(self):
return Step.objects.filter(parent=self, belonging=self.belonging).order_by('order', 'title')
def render_step(self):
template = get_template('app_questionnaire/_step.html')
return template.render({'step': self})
I have one Step without parent and only one. I just wonder if it's possible to disable the EMPTY parent field in the Django admin after adding a Step ? I would like to add others Step with a parent but not another Step without a parent. I don't know if you know what I mean :)
Thank you in advance !
I am not exactly getting what you want to do but yahh you can exclude field or declare particular field as read only:
class StepOver(admin.TabularInline):
model = Step
exclude = ['parent']
readonly_fields=('parent', )
You can do this by overriding the get_form method in you ModelAdmin:
class StepModelAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
form = super(StepModelAdmin, self).get_form(request, obj, **kwargs)
if Step.objects.count() > 1:
# this will hide the null option for the parent field
form.base_fields['parent'].empty_label = None
return form
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With