Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket objects need to have a primary key value before you can access their tags #890

Open
UnLuckyAki opened this issue Feb 26, 2024 · 1 comment
Labels

Comments

@UnLuckyAki
Copy link

Greetings!
In DRF on creating new object get a ValueError:
Ticket objects need to have a primary key value before you can access their tags
Will be apreciate for any ideas where I do wrong
MyModel:

class Ticket(models.Model):
    ...
    tags = TaggableManager(blank=True)
    ...

MySerializer:

class TicketSerializer(TaggitSerializer, serializers.ModelSerializer):
  up_datetime = serializers.DateTimeField(format="%H:%M:%S %d-%m-%Y", read_only=True)
  text = serializers.CharField(write_only=True)
  tags = TagListSerializerField(required=False)

  class Meta:
      model = Ticket
      fields = ['id', 'subject', 'text', 'status', 'category', 'customer', 'priority',  'current_user', 'up_datetime', 'tags']
      read_only_fields = ['id', 'status']

  def to_representation(self, instance):
      self.fields['current_user'] = UserSerializer(read_only=True)
      self.fields['status'] = TicketStatusSerializer(read_only=True)
      self.fields['category'] = TicketCategorySerializer(read_only=True)
      self.fields['priority'] = SerializerMethodField(read_only=True)
      self.fields['customer'] = TicketCustomerSerializer(read_only=True)
      return super().to_representation(instance)

  def get_priority(self, obj):
      return {'id': obj.priority, 'name': obj.get_priority_display()}

MyViewSet:

class TicketViewSet(NoDestroyViewSet):
    queryset = Ticket.objects.all()
    filter_backends = [DjangoFilterBackend]
    filterset_class = TicketFilterSet

    def get_serializer_class(self):
        if self.detail:
            return TicketDetailSerializer
        else:
            return TicketSerializer

    def perform_create(self, serializer):
        serializer.save(creator=self.request.user)

    def perform_update(self, serializer):
        serializer.save(updator=self.request.user)
@rtpg rtpg added the docs label Mar 7, 2024
@rtpg
Copy link
Contributor

rtpg commented Mar 7, 2024

@UnLuckyAki sorry, do you have a stacktrace available of your error, so we can see where you are hitting this?

Generally speaking, you can't get the tags of an object before saving the object. Why? Because in order to get the tags of an object we have to do a lookup by the object's primary key. And before you save an object, it doesn't have a primary key, so...

Given your code seemingly not doing anything too weird... this feels like something where maybe we want to have some special-case code in the serialisers, to make things smooth. After all, if an object isn't saved, we can probably say there are no tags. But I am curious about what the stack trace is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants