如何使用初始数据预填充作为GenericRelationship的模型?

时间:2013-01-04 13:58:33

标签: django django-models django-forms

我有一个针对通用模型的CreateView,我正在尝试使用与模型UpstreamClientModel(content_type,object_id,content_object)相关的数据预先填充它。我的问题是......如果我从AddressForm中排除这些字段,我在哪里填写它们?我在想,也许CreateView的get_initial()就是答案,但我不太确定:

class UpstreamClientModel(models.Model):
    users = models.ManyToManyField(User, related_name='owns', 
            verbose_name='users')
    client_name = models.CharField(max_length=30, unique=True)
    url = models.URLField(unique=True, null=False, 
            help_text="The client's main URL")
    phones = generic.GenericRelation(Phone)
    address = generic.GenericRelation(Address, 
            verbose_name='Postal address')
    social_media = generic.GenericRelation(SocialMediaAccountModel,
            verbose_name='Social media account')
    language = models.CharField(max_length=7, choices=LANGUAGES)
    is_active = models.BooleanField('Active client', default='True')
    tags = TaggableManager(verbose_name='industry', 
            help_text='comma-separated values, or "uncategorized"')

    def __unicode__(self):
        return self.client_name


class Address(models.Model):
    recipient = models.CharField(max_length=45)
    address_line_1 = models.CharField('Address line 1', max_length=45)
    address_line_2 = models.CharField('Address line 2', 
            max_length=45, blank=True, help_text='only if needed')
    town_city = models.CharField('Town/City', max_length=50)
    state_province = models.CharField('State/Province', max_length=45,
            blank=True)
    post_code = models.CharField('Postal Code', max_length=15)
    country = models.CharField(max_length=20, blank=True)
    # generic relationships so I can attach to other objects later on
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    def __unicode__(self):
        return '%s, %s, %s' % (
                self.address_line_1, 
                self.town_city, 
                self.post_code
            )

    class Meta:
        verbose_name = 'Postal address'
        verbose_name_plural = 'Postal addresses'
        unique_together = ('recipient', 'address_line_1',
                'town_city', 'post_code')

###### view (r'^clients/(?P<pk>\d+)/address/$')

class ClientAddressView(LoginRequiredMixin, AdminOrCoreRequiredMixin, CreateView):
    form_class = AddressForm
    template_name = 'clients/address_form.html'
    success_url = '/clients/'

    def get_initial(self):
        initial = super(ClientAddressView, self).get_initial()
        # Copy the dictionary so we don't accidentally change a mutable dict
        initial = initial.copy()
        client = UpstreamClientModel.objects.get(pk=self.kwargs['pk'])
        content_type = ContentType.objects.get_for_model(client)
        initial['content_type'] = content_type
        initial['object_id'] = self.kwargs['pk']
        return initial

这有效但显示不应公开的字段。如果我想从表单中排除content_type和object_id,我无法用get_initial()填充它们

0 个答案:

没有答案