48291

How to get a field of a foreign key Model appear in a Django-Serializer?

Question:

I am building a Django application that exposes a REST API by which users can query 2 of my application's models. I'm following the instructions <a href="http://www.django-rest-framework.org/tutorial/quickstart#quickstart" rel="nofollow"><strong>here</strong></a>.

My two models are:

<ol><li>The Django User model from django.contrib.auth</li> <li>

The model shown below.

class Profile(models.Model): user = models.OneToOneField(User) </li> </ol>

My Serialiazers are as follows:

class UserSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = User fields = ('url', 'username', 'email', ) class ProfileSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Profile fields = ('id', 'slug', 'user',)

I can see that this is working when I hit the APIs from the command-line:

% curl -H 'Accept: application/json; indent=4' -u root:MyPassword http://127.0.0.1:3001/api/profiles/60/ { "id": 60, "slug": "myprofile", "user": "http://127.0.0.1:3001/api/users/16/" } % curl -H 'Accept: application/json; indent=4' -uroot:MyPassword http://127.0.0.1:3001/api/users/16/ { "url": "http://127.0.0.1:3001/api/users/16/", "username": "myUser", "email": "myemail@gmail.com" }

What I would like to know are two things:

<ol><li>How do I change my Profile's serializer such that the user's username appears in the serialized profile?</li> <li>How can I expose this API publicly so it works even without the root/password login?</li> </ol>

Answer1:

Question #1 Answered correctly here: <a href="https://stackoverflow.com/questions/20040750/how-to-include-in-queryset-details-fields-of-a-foreign-key-django-and-rest-api?rq=1" rel="nofollow">How to include in queryset details fields of a foreign key (django and rest_api)</a>

Question #2 Re-asked more clearly here: <a href="https://stackoverflow.com/questions/22004281/how-are-permissions-to-access-to-django-rest-api-managed" rel="nofollow">How are permissions to access to Django REST API managed?</a>

Recommend

  • django-orm : How to update one-to-one relation field value
  • django - get list of objects by filtering a list of objects
  • django reverse lookup query within three tables
  • Convert OneToOneField to MultipleTableInheritance
  • Django Creating a Custom User with the Django Rest Framework
  • Using Django JSON serializer for object that is not a Model
  • Optimize finding all classes implementing IInterface and those explicitly implementing it with a spe
  • Django Rest Framework “A valid integer is required.”?
  • Spark Runs in Local but not in YARN
  • Conditional serialization with protobuf-net
  • Embedding a Google map
  • jinja2 template not found and internal server error
  • How secure are apple APNS push notifications?
  • Why does the font in these TD elements render at different sizes?
  • Rails Find when some params will be blank
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Jquery UI tool tip close icon
  • Dialing with Intent.ACTION_CALL stopps at # in phone number
  • Highlight one bar in a series in highcharts?
  • Lost migrations and Azure database is now out of sync
  • How can I send an e-mail from a vbs script
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Is there any way to access browser form field suggestions from JavaScript?
  • Android screen density dpi vs ppi
  • Dynamically accessing properties of knockoutjs observable array
  • Can Jackson SerializationFeature be overridden per field or class?
  • script to move all files from one location to another location
  • WinForms: two way TextBox problem
  • Can I make an Android app that runs a web view in Chrome 39?
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Circular dependency while pushing http interceptor
  • AngularJs get employee from factory
  • How to set the response of a form post action to a iframe source?
  • how does django model after text[] in postgresql [duplicate]
  • Change div Background jquery
  • Turn off referential integrity in Derby? is it possible?
  • Authorize attributes not working in MVC 4
  • Busy indicator not showing up in wpf window [duplicate]
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF