2

我有一个通用模型的 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() 填充它们

4

0 回答 0