Сегодня я изучаю Graphql и Graphene-Django.Я мог бы запросить и преобразовать простую таблицу, но не смог изменить, когда таблица имеет отношение внешнего ключа.У меня есть такая модель
class Company(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
name = models.CharField(max_length=150, blank=False, null=False)
slug = models.SlugField()
email = models.EmailField(max_length=150, blank=False, null=False)
class Brand(models.Model):
ZERO = 'Zero'
ZEROTOTEN = 'ZTT'
TENTOTWENTY = 'TTT'
NUMBER_OF_FRANCHISES = (
(ZERO, '0'),
(ZEROTOTEN, '0 - 10'),
(TENTOTWENTY, '10 - 20'),
)
company = models.ForeignKey(Company, related_name='company_brand', on_delete=models.CASCADE)
name = models.CharField(max_length=150, blank=False, null=False)
franchising_date = models.DateTimeField()
number_of_franchises = models.CharField(max_length=5, choices=NUMBER_OF_FRANCHISES, default=None)
class BusinessModel(models.Model):
company = models.ForeignKey(Company, related_name='company_business_model', on_delete=models.CASCADE)
industry = models.ForeignKey(Industry, null=True, related_name='industry', on_delete=models.SET_NULL)
segments = models.ForeignKey(Segment, on_delete=models.SET_NULL, null=True)
total_investment = models.CharField(max_length=50, choices=TOTAL_INVESTMENT, default=None)
franchise_fee = models.CharField(max_length=50, choices=FRANCHISE_FEE, default=None)
С такой моделью у меня есть следующая схема с типом ввода, но я не знаю, как мне создать таблицу для бренда и бизнес-модели, так как они связаны сКомпания.
class CompanyInput(graphene.InputObjectType):
name = graphene.String(description='Name of your company')
email = graphene.String(description='Email of your company')
phone_number = graphene.String(description='Phone number of your company')
director = graphene.String(description='Director of your company')
franchise_head = graphene.String(description='Franchise Head of your company')
class BrandInput(graphene.InputObjectType):
company = graphene.List(CompanyInput)
name = graphene.String()
franchising_date = graphene.String()
number_of_franchises = graphene.String()
class BusinessModelInput(graphene.InputObjectType):
company = graphene.List(CompanyInput)
industry = graphene.List(IndustryInput)
segments = graphene.String(SegmentInput)
total_investment = graphene.String()
franchise_fee = graphene.String()
# only a table for company can be created with this way but I have no idea on how can i create brand and business model table either as they are
# linked to company. Because a company can have brand and business model.
class CreateCompany(graphene.Mutation):
class Arguments:
input = CompanyInput(description="These fields are required", required=True)
class Meta:
description = "Creates a new company"
# model = models.Company
errors = graphene.String()
company = graphene.Field(CompanyNode)
@staticmethod
def mutate(root, info, input=None):
if not info.context.user.is_authenticated:
return CreateCompany(errors=json.dumps('Please Login to list your company'))
company = models.Company.objects.create(owner=info.context.user,
name=input.name,
email=input.email,
)
return CreateCompany(company=company, errors=None)
Любая помощь будет оценена.Я надеюсь, что этот пример охватит большинство тем и для таких людей, как я.