0

这是我的模型,它使用通用外键 im 使用 SmallUUIDFieldpos_idobject_id

class POSRecord(models.Model):
    id = SmallUUIDField(default=uuid_default(), primary_key=True, db_index=True, editable=False, verbose_name='ID')

    # FK to a POS integration (e.g., SquareCredentials)
    pos_content_type = models.ForeignKey('contenttypes.ContentType',
        related_name='pos_record_integration',
        on_delete=models.CASCADE)
    pos_id = SmallUUIDField()
    pos = GenericForeignKey('pos_content_type', 'pos_id')

    # FK to anything in our system (e.g., Item, ModifierList, Location)
    object_content_type = models.ForeignKey('contenttypes.ContentType',
        related_name='pos_record_object',
        on_delete=models.CASCADE)
    object_id = SmallUUIDField()
    object = GenericForeignKey('object_content_type', 'object_id')

    # Raw JSON data from the POS api
    data = JSONField()

POSRecord.objects.get_or_create()方法上,我将从 squareup 的 api 获得的 id 传递给密钥object_id,我得到ValueError: bytes is not a 16-char string. 我试图将POSRecord模型pos_id值更改为models.PositiveIntegerField()但随后出现此错误。ValueError: invalid literal for int() with base 10: 4JSZ539TSJ5GEIMW43FFNQXF

    def sync_item(self, square_item):
        item_content_type = ContentType.objects.get_for_model(Item)
        square_credential_content_type = ContentType.objects.get_for_model(SquareCredential)
        print(square_item.id)
        print(self.credentials.token)
        # Fetch our opy of the POS data
        try:
            record = POSRecord.objects.get_or_create(
                pos_content_type=square_credential_content_type,
                pos_id=self.credentials.token,
                object_content_type=item_content_type,
                object_id=square_item.id,
            )
        except POSRecord.DoesNotExist:
            record = POSRecord(pos=self.credentials)

        # Update our record with the fresh data
        record.data = square_item
        print(record)
        print(record.objects)
        # Get or instantiate our own version of the object 
        item = record.object
        if item is None:
            item = Item(tenant=self.tenant)
        print(item)
        # Update our version with the POS data
        item.name = record.data['name']
        item.decription = record.data['decription']

        # Save the world.
        item.save()
        if not record.object:
            record.object = item
        record.save()
4

0 回答 0