Admin - formset validation with counting checked elements

90 views
Skip to first unread message

galgal

unread,
Sep 22, 2011, 9:42:56 AM9/22/11
to django...@googlegroups.com
I make a mini poll system. To each question choices are related via FK. In admin I use Inline choices. Each choice has "correct" field (Boolean). When saving a poll I need to check if there is minimum 1 choice with "correct" selected. Which function in admin I must use, to do that validation?

BILLION Sébastien

unread,
Sep 22, 2011, 9:53:40 AM9/22/11
to django...@googlegroups.com
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/E_pX2IfyCBcJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

Try something like that:

poll = get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = poll.choice_set.get(pk=request.POST['choice'])
correct = selected_choice.correct
if correct:
do something
except (KeyError, Choice.DoesNotExist):

return render_to_response('youtemplate', {
'poll': poll,
'error_message': "Please select a choice",
})

--
BILLION Sébastien

Un geek averti en vaut 10

http://www.sebastienbillion.com/ <http://www.sebastienbillion.com/>

galgal

unread,
Sep 22, 2011, 9:56:09 AM9/22/11
to django...@googlegroups.com
But I need to do checks in Admin, not my view. I can't find a method to validate in Admin.

BILLION Sébastien

unread,
Sep 22, 2011, 10:05:35 AM9/22/11
to django...@googlegroups.com
Le jeudi 22 septembre 2011 11:56:09, galgal a écrit :
> But I need to do checks in Admin, not my view. I can't find a method
> to validate in Admin.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/rRHhnv1GGfEJ.

> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

Sorry,

in models.py:

class Poll(models.Model):
question = models.CharField(max_length=200)
def __unicode__(self):
return self.question

class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
correct = models.BooleanField()
def __unicode__(self):
return self.choice

in admin.py

class ChoiceInline(admin.TabularInline):
model = Choice
extra = 4

class PollAdmin(admin.ModelAdmin):
inlines = [ChoiceInline]

admin.site.register(Poll, PollAdmin)

galgal

unread,
Sep 22, 2011, 10:13:20 AM9/22/11
to django...@googlegroups.com
I have my model done, the only thing is - how to validate if 1 or more checkboxes are checked. It should be done in admin - choices are displayed via Inline.

BILLION Sébastien

unread,
Sep 22, 2011, 10:19:22 AM9/22/11
to django...@googlegroups.com
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/ZbU-_CGCZsEJ.

> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

Sorry one more time!

add this to your admin.py

class PollAdmin(admin.ModelAdmin):
inline =[ChoiceInline]
#check bafore save
def save_model(self, request, obj, form, change):
for choice in obj.choice_set.all():
if choice.correct == True:
print "ok"
obj.save()

Reply all
Reply to author
Forward
0 new messages