Wednesday, September 4, 2013

ValueError: Cannot create form field for 'room_setup' yet, because its related model 'main_page.roomsetup' has not been loaded yet

Hi,

given the following models and model form I get the error in the subject, apparently because the related model "roomsetup" is defined *after* the "RoomsForm" form and is not loaded at the time the form is created.

class rooms(models.Model):
    [.... lots of other fields]
    room_setup = models.ForeignKey('roomsetup', verbose_name='Room setup', blank=False, default='')

    def __unicode__(self):
        return self.name


class RoomsForm(ModelForm):
    class Meta:
        model = rooms
        fields = ('active', 'location', 'level', 'room_setup', 'name', 'daylight', 'area', 'height', 'notes',)

.... other models and forms...

class roomsetup(models.Model):
    [....lots of fields]

    def __unicode__(self):
        return self.name

If I move the RoomsForm declaration *below* the room setup class, everything works fine, but I would like to keep the model and its associated form together in my code (this makes sense?).

So is there another  way how can I avoid this situation?

Thanks
Thomas

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment