石墨烯-python 无法识别“content_type”场
graphene-python doesn't recognize ´content_type´ field
我第一次尝试使用“石墨烯-python”,到目前为止我已经能够让它工作,但我发现“石墨烯-python”没有无法识别引用 ContentType 模型的 ForeignKey 字段。
这是我的模型:
class ReservationComponent(models.Model):
reservation = models.ForeignKey(Reservation, on_delete=models.PROTECT, related_name='components', verbose_name=_("Reservation"))
dertour_bk = models.CharField(null=True, blank=True, max_length=15, verbose_name=_("Dertour Bk"))
day = models.DateField(verbose_name=_('Day'))
content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
comment = models.TextField(null=True, blank=True, verbose_name=_("Comment"))
is_invoiced = models.BooleanField(default=False, verbose_name=_("Is Invoiced"))
这是我的schemas.py:
import graphene
from graphene_django.types import DjangoObjectType
from ReservationsManagerApp.models import Reservation, ReservationComponent
from InvoicesManagerApp.models import Invoice, InvoiceEntry, InvoiceEntryComponent
from PaymentsManagerApp.models import Payment, PaymentReservationComponent
class ReservationType(DjangoObjectType):
class Meta:
model = Reservation
class ReservationComponentType(DjangoObjectType):
class Meta:
model = ReservationComponent
class InvoiceType(DjangoObjectType):
class Meta:
model = Invoice
class InvoiceEntryType(DjangoObjectType):
class Meta:
model = InvoiceEntry
class InvoiceEntryComponentType(DjangoObjectType):
class Meta:
model = InvoiceEntryComponent
class PaymentType(DjangoObjectType):
class Meta:
model = Payment
class PaymentReservationComponentType(DjangoObjectType):
class Meta:
model = PaymentReservationComponent
class Query(object):
all_reservations = graphene.List(ReservationType)
all_reservation_components = graphene.List(ReservationComponentType)
all_invoices = graphene.List(InvoiceType)
all_invoice_components = graphene.List(InvoiceEntryType)
all_invoice_entries_components = graphene.List(InvoiceEntryComponentType)
all_payment = graphene.List(PaymentType)
all_payment_reservation_components = graphene.List(PaymentReservationComponentType)
def resolve_all_reservations(self, info, **kwargs):
return Reservation.objects.all()
def resolve_all_reservation_components(self, info, **kwargs):
return ReservationComponent.objects.select_related('reservation').all()
def resolve_all_invoice_entries_components(self, info, **kwargs):
return InvoiceEntryComponent.objects.select_related('reservation_component').all()
def resolve_all_payment_reservation_components(self, info, **kwargs):
return PaymentReservationComponent.objects.select_related('reservation_component').all()
并且,在此图像中,您可以看到模型“ReservationComponent”的字段“content_type”未被 graphene-python 接受,即使它存在于模型中。
我不知道我是否遗漏了任何设置或其他内容。
这是因为您没有DjangoObjectType
for django 的ContentType
模型。
您可以通过以下方式创建它,
from graphene_django import DjangoObjectType
from django.contrib.contenttypes.models import ContentType
class ContentObjectType(DjangoObjectType):
class Meta:
model = ContentType
根据 documentation,您必须为子类 DjangoObjectType
的每个模型创建一个类型
我第一次尝试使用“石墨烯-python”,到目前为止我已经能够让它工作,但我发现“石墨烯-python”没有无法识别引用 ContentType 模型的 ForeignKey 字段。
这是我的模型:
class ReservationComponent(models.Model):
reservation = models.ForeignKey(Reservation, on_delete=models.PROTECT, related_name='components', verbose_name=_("Reservation"))
dertour_bk = models.CharField(null=True, blank=True, max_length=15, verbose_name=_("Dertour Bk"))
day = models.DateField(verbose_name=_('Day'))
content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
comment = models.TextField(null=True, blank=True, verbose_name=_("Comment"))
is_invoiced = models.BooleanField(default=False, verbose_name=_("Is Invoiced"))
这是我的schemas.py:
import graphene
from graphene_django.types import DjangoObjectType
from ReservationsManagerApp.models import Reservation, ReservationComponent
from InvoicesManagerApp.models import Invoice, InvoiceEntry, InvoiceEntryComponent
from PaymentsManagerApp.models import Payment, PaymentReservationComponent
class ReservationType(DjangoObjectType):
class Meta:
model = Reservation
class ReservationComponentType(DjangoObjectType):
class Meta:
model = ReservationComponent
class InvoiceType(DjangoObjectType):
class Meta:
model = Invoice
class InvoiceEntryType(DjangoObjectType):
class Meta:
model = InvoiceEntry
class InvoiceEntryComponentType(DjangoObjectType):
class Meta:
model = InvoiceEntryComponent
class PaymentType(DjangoObjectType):
class Meta:
model = Payment
class PaymentReservationComponentType(DjangoObjectType):
class Meta:
model = PaymentReservationComponent
class Query(object):
all_reservations = graphene.List(ReservationType)
all_reservation_components = graphene.List(ReservationComponentType)
all_invoices = graphene.List(InvoiceType)
all_invoice_components = graphene.List(InvoiceEntryType)
all_invoice_entries_components = graphene.List(InvoiceEntryComponentType)
all_payment = graphene.List(PaymentType)
all_payment_reservation_components = graphene.List(PaymentReservationComponentType)
def resolve_all_reservations(self, info, **kwargs):
return Reservation.objects.all()
def resolve_all_reservation_components(self, info, **kwargs):
return ReservationComponent.objects.select_related('reservation').all()
def resolve_all_invoice_entries_components(self, info, **kwargs):
return InvoiceEntryComponent.objects.select_related('reservation_component').all()
def resolve_all_payment_reservation_components(self, info, **kwargs):
return PaymentReservationComponent.objects.select_related('reservation_component').all()
并且,在此图像中,您可以看到模型“ReservationComponent”的字段“content_type”未被 graphene-python 接受,即使它存在于模型中。
我不知道我是否遗漏了任何设置或其他内容。
这是因为您没有DjangoObjectType
for django 的ContentType
模型。
您可以通过以下方式创建它,
from graphene_django import DjangoObjectType
from django.contrib.contenttypes.models import ContentType
class ContentObjectType(DjangoObjectType):
class Meta:
model = ContentType
根据 documentation,您必须为子类 DjangoObjectType