это моя модель, которая использует общий внешний ключ, используя SmallUUIDField для pos_id
и object_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(
я передаю id, который я получаю из api squareup, ключу object_id
и получаю E 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()