使用Django Graphene更新/创建变异

时间:2019-04-13 12:54:43

标签: python django django-models graphql graphene-python

我正在努力寻找一个好的例子或最佳实践,以创建一种优雅的方式来更新/创建django和石墨烯相关模型的突变。

假设您有一个模型地址与(城市,州,邮政编码)具有OneToOne关系

class ZipCode(models.Model):
    zip = models.IntegerField(_("Zip"), blank=False)

    def __str__(self):
        return self.zip

    class Meta:
        verbose_name_plural = _('Zip codes')


class State(models.Model):
    state = models.CharField(_("State"), max_length=12, blank=False)

    def __str__(self):
        return self.state

    class Meta:
        verbose_name_plural = _('States')


class City(models.Model):
    city = models.CharField(_("City"), max_length=12, blank=False)

    def __str__(self):
        return self.city

    class Meta:
        verbose_name_plural = _('Cities')


class Address(models.Model):
    line_1 = models.CharField(_("Address Line 1"), max_length=256, blank=True)
    line_2 = models.CharField(_("Address Line 2"), max_length=256, blank=True)
    state = models.OneToOneField(
        State,
        related_name=_("address_state"),
        verbose_name=_("State"),
        on_delete=models.CASCADE,
        null=True
    )
    city = models.OneToOneField(
        City,
        related_name=_("address_city"),
        verbose_name=_("City"),
        on_delete=models.CASCADE,
        null=True
    )
    zipcode = models.OneToOneField(
        ZipCode,
        related_name=_("address_zipcode"),
        verbose_name=_("ZipCode"),
        on_delete=models.CASCADE,
        null=True)

    def __str__(self):
        return self.id

    class Meta:
        abstract = True
        verbose_name_plural = _("Addresses")

您如何以一种优雅的方式创建一个可以接受如下查询的突变:

mutation {
  updateAddress(addressData: {line_1: "Test"}, zipData: { zip: "12345" }, cityData: {city: "New York"}, stateData: { state: "NY" }) { ... }

0 个答案:

没有答案