Tags down


django restful api - how to serialize foreign keys

By : Jack Tibeau
Date : October 16 2020, 08:10 PM
I think the issue was by ths following , In the corresponding example, they've used to return the Album's data whereas you are trying to return/print Track's data.
So, if you try as below, it will print/return the data as described/shown in the DRF doc
code :
def index(request):
    if Track.objects.all().count() == 0:
        album = Album.objects.create(album_name='something', artist='John')

        print(AlbumSerializer(album).data)  # this will print the data as explianed in the doc

        Track.objects.create(album=album, order=1, title='something', duration=1)

    return render(request, 'index.html')
class AlbumNewSerializer(serializers.ModelSerializer):
    class Meta:
        model = Album
        fields = ('album_name', 'artist')

class TrackSerializer(serializers.ModelSerializer):
    album = AlbumNewSerializer()

    class Meta:
        model = Track
        fields = ('order', 'title', 'duration', 'album')

Share : facebook icon twitter icon

Restful Web Services - Maintaining Foreign Keys

By : eponymous
Date : March 29 2020, 07:55 AM
Any of those help Short Answer: country_id is null, so this looks like a database/persistence issue. You probably didn't set the Country for the StateProvince (or add the StateProvince to the Country - haven't seen your code so I don't know how you're mapping things).
Long Answer:

In Django, what's the best way to set up optional foreign keys if most of the foreign keys are blanks?

By : hogetaka
Date : March 29 2020, 07:55 AM
With these it helps From questions like this one, I know that the recommended way to make optional foreign keys in django is to set null=True, blank=True. That way, the value of the foreign key does not have to be set. , You can simulate a nullable foreign key by using a third table:
code :
class City(models.Model):
    name = models.CharField(max_length=80)

class Person(models.Model):
    name = models.CharField(max_length=80)

class PersonCity(models.Model):
    person = models.ForeignKey(Person, unique=True)
    city = models.ForeignKey(City)
city = person.personcity_set().first().city

Django REST serialize output - group by foreign keys

By : Rafael Jp
Date : March 29 2020, 07:55 AM
wish helps you The only thing i can find it's postgres specific aggregation function ArrayAgg
You can use it like this:
code :
from django.contrib.postgres.aggregates import ArrayAgg


# example output: 
# [
#     {
#         'menu__name': 'menu1',
#         'items': ['item1', 'item2']
#     },
#     {
#         'menu__name': 'menu2',
#         'items': ['item3', 'item4']
#     },
# ]
  ARRAY_AGG("appname_item"."name") AS "items"
FROM "appname_item"
  INNER JOIN "appname_menu" ON ("appname_item"."menu_id" = "appname_menu"."id")
WHERE "appname_item"."restaurant_id" = 1
GROUP BY "appname_menu"."name"

Django REST Framework serialize multiple foreign keys related between

By : user2956185
Date : March 29 2020, 07:55 AM

Limit number of foreign keys based on how many foreign keys refer to that model in Django?

By : Adam Holovics
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Using pre_save signal, whenever you try creating a new Resident instance, you can first check the total number of available beds in the room at where the resident will stay:
Privacy Policy - Terms - Contact Us © voile276.org