ジェネリック モデルの 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() で埋めることができません