在 Django 中手动保存 OnetoOne 字段
Saving OnetoOne field manually in django
我有以下型号:
class Person(User):
first_name = models.CharField(verbose_name=_('first name'), max_length=30)
last_name = models.CharField(verbose_name=_('last name'), max_length=30)
class Service(models.Model):
person = models.ForeignKey(Person, related_name='services')
price_from = models.DecimalField(_('price form'), max_digits=10, decimal_places=2, validators=[MinValueValidator(Decimal('0.01'))])
price_to = models.DecimalField(_('price to'), max_digits=10, decimal_places=2, validators=[MinValueValidator(Decimal('0.01'))])
class WorkPlace(models.Model):
service = models.OneToOneField('Service', related_name='work_place', primary_key=True)
city = CharField(verbose_name=_('city'), max_length=255)
street = CharField(verbose_name=_('street'), max_length=255)
我还在管理员中注册了 Person
并使 Service
成为内联管理员。
由于设计原因,城市和地址作为多值字段输入。
问题是我无法手动保存 WorkPlace。
以下是表格:
class WorkPlaceForm(forms.ModelForm):
class Meta:
model = WorkPlace
fields = '__all__'
class PersonForm(forms.ModelForm):
class Meta:
model = Person
fields = '__all__'
class ServiceForm(forms.ModelForm):
class Meta:
fields = '__all__'
model = Service
multi_work_place = MyMultiValueField()
def save(self, commit=True):
service = super(ServiceForm, self).save(commit=commit)
if self.cleaned_data['multi_work_place']:
work_place = WorkPlace(**{
'city': self.cleaned_data['multi_work_place'][0],
'street': self.cleaned_data['multi_work_place'][1],
})
# when there is brand new object is created, there is no service.pk
work_place.service = service # how???
work_place.save()
return service
此外,如果我在新对象上写 service = super(ServiceForm, self).save(commit=True)
,这将引发错误,因为没有创建 Person
。
我该如何解决这个问题?回想一下,我在管理部门工作。
试试这个:
class ServiceForm(forms.ModelForm):
class Meta:
fields = '__all__'
model = Service
multi_work_place = MyMultiValueField()
def save(self, commit=True):
service = save_instance(self, self.instance, self._meta.fields,
fail_message, commit, self._meta.exclude,
construct=False)
service.save()
if self.cleaned_data['multi_work_place']:
work_place = WorkPlace(**{
'city': self.cleaned_data['multi_work_place'][0],
'street': self.cleaned_data['multi_work_place'][1],
})
# when there is brand new object is created, there is no service.pk
work_place.service = service
work_place.save()
return service
class ServiceForm(forms.ModelForm):
class Meta:
fields = '__all__'
model = Service
multi_work_place = MyMultiValueField()
def save(self, commit=True):
service = super(ServiceForm, self).save(commit=False)
service.member_id = service.member.pk # here is the key command
service.save()
if self.cleaned_data['multi_work_place']:
work_place = WorkPlace(**{
'city': self.cleaned_data['multi_work_place'][0],
'street': self.cleaned_data['multi_work_place'][1],
})
# when there is brand new object is created, there is no service.pk
work_place.service = service # how???
work_place.save()
return service
我只需要将会员pk添加到服务模型中
我有以下型号:
class Person(User):
first_name = models.CharField(verbose_name=_('first name'), max_length=30)
last_name = models.CharField(verbose_name=_('last name'), max_length=30)
class Service(models.Model):
person = models.ForeignKey(Person, related_name='services')
price_from = models.DecimalField(_('price form'), max_digits=10, decimal_places=2, validators=[MinValueValidator(Decimal('0.01'))])
price_to = models.DecimalField(_('price to'), max_digits=10, decimal_places=2, validators=[MinValueValidator(Decimal('0.01'))])
class WorkPlace(models.Model):
service = models.OneToOneField('Service', related_name='work_place', primary_key=True)
city = CharField(verbose_name=_('city'), max_length=255)
street = CharField(verbose_name=_('street'), max_length=255)
我还在管理员中注册了 Person
并使 Service
成为内联管理员。
由于设计原因,城市和地址作为多值字段输入。
问题是我无法手动保存 WorkPlace。
以下是表格:
class WorkPlaceForm(forms.ModelForm):
class Meta:
model = WorkPlace
fields = '__all__'
class PersonForm(forms.ModelForm):
class Meta:
model = Person
fields = '__all__'
class ServiceForm(forms.ModelForm):
class Meta:
fields = '__all__'
model = Service
multi_work_place = MyMultiValueField()
def save(self, commit=True):
service = super(ServiceForm, self).save(commit=commit)
if self.cleaned_data['multi_work_place']:
work_place = WorkPlace(**{
'city': self.cleaned_data['multi_work_place'][0],
'street': self.cleaned_data['multi_work_place'][1],
})
# when there is brand new object is created, there is no service.pk
work_place.service = service # how???
work_place.save()
return service
此外,如果我在新对象上写 service = super(ServiceForm, self).save(commit=True)
,这将引发错误,因为没有创建 Person
。
我该如何解决这个问题?回想一下,我在管理部门工作。
试试这个:
class ServiceForm(forms.ModelForm):
class Meta:
fields = '__all__'
model = Service
multi_work_place = MyMultiValueField()
def save(self, commit=True):
service = save_instance(self, self.instance, self._meta.fields,
fail_message, commit, self._meta.exclude,
construct=False)
service.save()
if self.cleaned_data['multi_work_place']:
work_place = WorkPlace(**{
'city': self.cleaned_data['multi_work_place'][0],
'street': self.cleaned_data['multi_work_place'][1],
})
# when there is brand new object is created, there is no service.pk
work_place.service = service
work_place.save()
return service
class ServiceForm(forms.ModelForm):
class Meta:
fields = '__all__'
model = Service
multi_work_place = MyMultiValueField()
def save(self, commit=True):
service = super(ServiceForm, self).save(commit=False)
service.member_id = service.member.pk # here is the key command
service.save()
if self.cleaned_data['multi_work_place']:
work_place = WorkPlace(**{
'city': self.cleaned_data['multi_work_place'][0],
'street': self.cleaned_data['multi_work_place'][1],
})
# when there is brand new object is created, there is no service.pk
work_place.service = service # how???
work_place.save()
return service
我只需要将会员pk添加到服务模型中