Destiny Franks
Destiny Franks

Reputation: 821

Django: Field 'object_id' expected a number but got 'fe2b1fd4313c'

I am getting this error while trying to save a model from the admin section using Django admin, this is the error Field 'object_id' expected a number but got 'id_b2cbfe2b1fd4313c'.. I am using django shortuuid package https://pypi.org/project/shortuuid/ to create id field in django, and i choose to use it because the inbuild UUID field keeps giving this error Django UUIDField shows 'badly formed hexadecimal UUID string' error? and the id looks like this id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True). What would be the problem witht the short uuid field.

Based on this, i quote:

If you filter on the ForeignKey, then Django will filter on the primary key of the target object, and that is normally an AutoField, unless you referred to another (unique) column, or defined another primary key as field.

But i dont know what the issue might be now

Models.py

class Channel(models.Model):
    id = ShortUUIDField( length=16, max_length=40, prefix="id_", alphabet="abcdefg1234", primary_key=True,)
    full_name = models.CharField(max_length=200) 
    user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True, related_name="channel")

Views.py

def channel_profile(request, channel_name):
    channel = Channel.objects.get(id=channel_name, status="active")
    context = {
        "channel": channel,
    }
    return render(request, "channel/channel.html", context)

Upvotes: 0

Views: 553

Answers (1)

Gaëtan GR
Gaëtan GR

Reputation: 1398

Your database is not sync with your migrations file, because your problem is easy to solve, Django is expecting an ID (integrer) and you are passing a string.

If you have not push your project into production you can delete the migration folder and migrate again, otherwise you need to update your migrations files to change the type for the UD field

Upvotes: 1

Related Questions