Hello

Thanks to several people in these forums i've managed to understand
how to make django queries when it comes to generic relations and how
to manage those objects in django admin.

Now my problems start getting more specific.

I have 3 models - polls, questions and choices that are related to
eachother via foreignkey. Poll has questions and questions have
choices. They all are related via generic relations to translations
table which holds all their translations. That means taht neither
polls, questions nor choices have any charfields. :

class Trans(models.Model):
        keel = models.ForeignKey(lang)
        content_type = models.ForeignKey(ContentType)
        object_id = models.PositiveIntegerField()
        content_object = generic.GenericForeignKey()
        name= models.CharField(max_length=500)
        def __unicode__(self):
                return unicode(self.name)
        class Meta:
                verbose_name_plural = 'Translations'
                verbose_name = 'Translation'

class Poll(models.Model):
        name = generic.GenericRelation(Trans)
        pub_date = models.DateTimeField('date published')
        active = models.BooleanField()
        def __unicode__(self):
                q = ContentType.objects.get_for_model(self)
                z = Trans.objects.exclude(keel=2).get(content_type__pk=q.id,
object_id=self.id)
                t = z.name
                return t

class Question(models.Model):
        TYPE_CHOICES = (
                ('1', 'Yks Vastus'),
                ('2', 'Mitu Vastust'),
                ('3', 'Tekstiväli'),
        )
        poll = models.ForeignKey(Poll)
        nr = models.IntegerField()
        name = generic.GenericRelation(Trans)
        qtype = models.CharField(max_length=1, choices = TYPE_CHOICES)
        def __unicode__(self):
                q = ContentType.objects.get_for_model(self)
                z = Trans.objects.exclude(keel=2).get(content_type__pk=q.id,
object_id=self.id)
                t = z.name
                return t

class Choice(models.Model):
        name = generic.GenericRelation(Trans)
        question = models.ForeignKey(Question)
        nr = models.IntegerField()
        def __unicode__(self):
                q = ContentType.objects.get_for_model(self)
                z = Trans.objects.exclude(keel=2).get(content_type__pk=q.id,
object_id=self.id)
                t = z.name
                return t

Now in Django admin I have This for managing & creating a poll:

class PollAdmin(admin.ModelAdmin):
        list_display = ['id', 'name', 'pub_date', ]
        ordering = ['pub_date']
        inlines = [ TranslationInline ]

With this i can enter poll name translations in available languages
for poll.

I Have similar class for managing Questions:
class QuestionAdmin(admin.ModelAdmin):
        list_display = ['id', 'nr', 'name', 'poll']
        inlines = [ TranslationInline, ChoiceInline ]

The problem is though, that while i can enter translations for
question name itself, the choiceinline only gives me NR field for
changing and does not give fields for entering choice translations.

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

Now as i understand i would have to change this choiceinline to
contain those translation fields for questionadmin. Do i need my own
formset for this, or can it be done someway simpler? I did read
through those formsets
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#model-formsets
but the examples there didnt help me much, because as i understand i
have to show fields from Trans model not Choice model.
Or would this (or something similar) help :
class ChoiceInline(admin.TabularInline):
    ChoiceFormSet = modelformset_factory(Transr, fields=('name',
'title'))

In addition to that i have trouble with translationinline:

class TranslationInline(generic.GenericTabularInline):
        model = Trans
        extra = lang.objects.count()
        verbose_name = "Translations"
        max_num = lang.objects.count()

I need exactly the amount of langugage objects of fields. Nothing
more, nothing less. And this extra & max_num does not work at all for
that. Do i need custom formset for that too?

Alan.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to