,)\": \"BusinessModel.company\" 必须是 \"Company\" 实例,python,django,django-models,django-views,graphene-python"/>

首页 > 解决方案 > 无法分配\"(,)\": \"BusinessModel.company\" 必须是 \"Company\" 实例

问题描述

我正在尝试更新商业模式。但是,我收到一个错误

无法分配 \"(,)\":\"BusinessModel.company\" 必须是 \"Company\" 实例。

我相信,我正在使用公司实例保存到以下公司,因为公司是 FK in BusinessModel

公司不是公司实例吗?这是我获取实例的辅助函数

def get_instance(_object, encoded_id, slug=False, otherwise=None):
    try:
        if slug:
            return _object.objects.get(slug=encoded_id)
        else:
            return _object.objects.get(pk=from_global_id(encoded_id)[1])
    except _object.DoesNotExist:
        return otherwise

这是我所做的

class BusinessModel(models.Model):

    ZERO = '0'
    ONETOFIFETYTHOUSAND = '1 - 50000'
    FIFETYTHOUSANDTOONELAKH = '50000 - 1Lakhs'

    TOTAL_INVESTMENT = (
        (ZERO, '0'),
        (ONETOFIFETYTHOUSAND, '1 - 50000'),
        (FIFETYTHOUSANDTOONELAKH, '50000 - 1Lakhs'),
    )
    FRANCHISE_FEE = TOTAL_INVESTMENT
    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)
    is_refundable = models.BooleanField(default=False)
    space = models.CharField(max_length=50, blank=False, null=False, help_text="space in square feet")  # choice field


class BusinessModelInput(graphene.InputObjectType):

    company = graphene.String()
    industry = graphene.String()
    segments = graphene.String()
    total_investment = graphene.String()
    franchise_fee = graphene.String()
    is_refundable = graphene.String()
    space = graphene.String()
    expanding_country = graphene.String()
    expanding_city = graphene.String()
    expanding_regions = graphene.String()


class UpdateBusinessModel(graphene.Mutation):

    class Arguments:

        input = BusinessModelInput(description="These fields are required", required=True)
        id = graphene.String(description="Id of business model", required=True)

    class Meta:

        description = "Update Business Model Mutation"

    errors = graphene.String()
    business_model = graphene.Field(BusinessModelNode)

    @staticmethod
    def mutate(root, info, **args):
        if not info.context.user.is_authenticated:
            return UpdateBusinessModel(errors=json.dumps('Please Login to list your brand'))
        try:
            print('args', args.get("id"))
            business_model_instance = get_instance(models.BusinessModel, args.get('id'))
            company = get_instance(models.Company, args.get('input')['company'], slug=True)
            print("company", company)
            industry = models.Industry.objects.get(slug=args.get('input')['industry'])
            segment = models.Segment.objects.get(slug=args.get('input')['segments'])
            if business_model_instance and company and industry:
                business_model_instance.company = company,
                business_model_instance.industry = industry,
                business_model_instance.segments = segment,
                business_model_instance.total_investment = args.get('input')['total_investment'],
                business_model_instance.franchise_fee = args.get('input')['franchise_fee'],
                business_model_instance.is_refundable = args.get('input')['is_refundable'],
                business_model_instance.space = args.get('input')['space'],
                print('business_model instance', business_model_instance)
                business_model_instance.save()
                return UpdateBusinessModel(business_model=business_model_instance, errors=None)
        except (models.BusinessModel.DoesNotExist, models.Company.DoesNotExist, models.Industry.DoesNotExist, models.Segment.DoesNotExist):
            return UpdateBusinessModel(errors=json.dumps('Company should be required'))

为什么我会收到错误消息?

标签: pythondjangodjango-modelsdjango-viewsgraphene-python

解决方案


在每个分配之后都有逗号,这会将值转换为元组。删除它们。

business_model_instance.company = company  # no comma

推荐阅读