代码之家  ›  专栏  ›  技术社区  ›  milan

无法分配\“(<Company:Waft Company>,)\”:\“BusinessModel.Company\”必须是\“Company\”实例

  •  -1
  • milan  · 技术社区  · 7 年前

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

    我相信,我是用公司实例来保存给以下公司的,因为公司是FK-in BusinessModel

    公司不是公司实例吗?下面是我的helper函数,用于获取实例

    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'))
    

    为什么我会出错?

    1 回复  |  直到 7 年前
        1
  •  3
  •   Daniel Roseman    7 年前

    在每个赋值后都有逗号,这会将值转换为元组。取下它们。

    business_model_instance.company = company  # no comma
    
    推荐文章