Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:59:11+00:00 2026-06-17T16:59:11+00:00

Problem I have a model with the following standard generic foreign key fields: content_type

  • 0

Problem

I have a model with the following standard generic foreign key fields:

content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
event_object = generic.GenericForeignKey('content_type', 'object_id')

According to REST framework’s docs, I can do the following to serialise this correctly:

class WhateverSerializer(serializers.ModelSerializer):
    event_object = serializers.RelatedField(source='event_object')

This works fine, however in two other related situations, I can’t get things working:

  1. I would like to use HyperlinkedRelatedField. This field requires the view_name argument, something I can’t declare since the view name varies with the related model. I solved this by using SerializerMethodField, instantiating a HyperlinkedIdentityField at runtime and returning its field_to_native method (see snippet below). This does not feel very elegant.
  2. I would like to nest the related object directly in the serialisation by saying event_object = SoAndSoSerializer(source='event_object'). The only solution I can see is to walk every *Serializer I have defined and check which has the correct model, then use that. Again, this does not feel very elegant.

Questions

is HyperlinkRelatedField meant to work across a generic relationship? Am I just making a mistake? Is there an obvious solution to picking the right *Serializer that I’m missing?

Code Snippet

The inelegant solution mentioned in bullet point 1 above:

class WhateverSerializer(DefaultSerializer):

    event_object_url = serializers.SerializerMethodField('get_related_object_url')
    # ...

    def get_related_object_url(self, obj):
        obj = obj.event_object
        default_view_name = '%(model_name)s-detail'
        format_kwargs = {
            'app_label': obj._meta.app_label,
            'model_name': obj._meta.object_name.lower()
        }
        view_name = default_view_name % format_kwargs
        s = serializers.HyperlinkedIdentityField(source=obj, view_name=view_name)
        s.initialize(self, None)
        return s.field_to_native(obj, None)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:59:12+00:00Added an answer on June 17, 2026 at 4:59 pm

    Your right, REST framework doesn’t support those use cases, and its not obvious to me what the design would look like if it did. You’d probably need an implicit registry of model->view (for the hyperlinked case) and model-> serializer (for the nested case) which I don’t think I’d be very keen on.

    The simplest way to do what you need is probably to subclass ManyRelatedField and create a custom field type, overriding to_native(self, obj) to serialize each object in the set exactly the way you want it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive got the following problem. I have a model called user which has a
I have an interesting problem with hibernate, the model looks like the following: @NamedQueries({
i've a problem with my tabularinline field. I have model like this class Product(models.Model):
I have following problem: have to model classes (posts and users). Main view is
I have the following model defined in .NET: Public Class Request <Key()> Public Property
I have a problem with CI. I have a model: public function Game($id) {
I have a problem with my MVC 4 Application. I have a Model with
I have problem with keeping my model (represented as List) in sync with a
Here is my problem: I have an array of model class(Let's say, 'addressModel' with
I got a simple problem in SQLAlchemy. I have one model in a table,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.