How to access content_type id of the Voting Model?

I want to access the id of content_type of the Voting model where user belongs to the Info model also.
If I check with it gives Object but I need id of the content_type which belongs to Voting for a specific user. How can I access that?

class Voting(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT, default=None)
    object_id = models.PositiveIntegerField(default=None)
    content_object = GenericForeignKey('content_type', 'object_id')
    Helpful = 0
    Unhelpful = 1
    voting = (
    (0, 'Helpful'),
    (1, 'Unhelpful')
    vote =  models.IntegerField(choices=voting, default=Helpful)
    class Meta:
        indexes = [
            models.Index(fields=['content_type', 'object_id']),  # Unique index on user+post
    submission_time = models.DateTimeField(, blank=True)
class Info(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    votes = GenericRelation(Voting, related_query_name="insight_vote")
    created_at = models.DateTimeField(, blank=True)
    updated = models.DateField(, blank=True)
    active = models.BooleanField(default=False)

To rephrase for clarity - you have an instance of Info, linked indirectly through User, to the model Voting.

You want the content_type field of that Voting model, correct?

What does your current query look like?

(Note, since Voting has a ForeignKey to User, there can be multiple instances of Voting that satisfies that condition.)

My current query look like this :

votinginfo = Info.objects.get(pk=1)
voter = Vote.objects.get(user=request.user)
content = ContentType.objects.get_for_model(Vote)
data = votinginfo.votes.filter(content_type=content, user=voter)

From here I want to query specific Info that was voted by a given user

Strictly speaking, based upon the information you’ve given:


But somehow I don’t think that’s the question you’re really trying to get answered.

It’s still not clear what you’re trying to achieve.

For example,
There is a forum post, which is Info Model. Any user can do the post. Any other user can Vote that post(either helpful or unhelpful). Now, I want to get the Vote done by supplied user id. With this:

I think we will get the post done by user. I want vote done by user instead.

Here :arrow_down: I am getting the user who voted.

voter = Voting.objects.get(user=request.user)

Now, I want to know the id of the Info for which user voted.

So what we’re missing here is the definition of the Vote Model.

How can I do that? Isnt it the correct way?

data = votinginfo.votes.filter(content_type=content, user=voter)

But its not giving me expected result.

I don’t know. I don’t understand your data structures. Your reliance upon generic foreign keys without any referent isn’t clear.

You say that you want instances of a Vote model, but you don’t show the definition for a Vote model. You now show a reference to a votinginfo object which appears to be an object of type Info - but you mentioned you don’t want an Info.

You don’t provide any definition as to the relationships among these models or any context of what they are or how they’re used to help us understand what the real issue is.

So all this is to say that I can’t answer your last question because I don’t know what you’re trying to do.

1 Like

I don’t know what you don’t know because its all data structure that I have and the votes has GenericRelation if you clearly see the question I posted. Question is very clear and consise and I believe that this is not big issue as well but I am stuck.

voter = Voting.objects.get(user=request.user)

Get the X vote which A user voted for 1 Info, its all what I want.