在django中手动保存OnetoOne字段

时间:2015-02-01 11:34:12

标签: django django-models django-forms django-admin

我有以下模特:

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而引发错误。

我该如何解决这个问题?回想一下,我在管理员工作。

2 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

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添加到服务模型