Saving related objects - python

I'm having some trouble saving related objects with Django Rest Framework. Here are my models
# models.py
class PowerStatus(models.Model):
status = models.CharField(max_length=50)
class VirtualMachine(models.Model):
power_status = models.ForeignKey(PowerStatus, verbose_name='Power status')
My serializers look like this:
# serializers.py
class PowerStatusSerializer(serializers.ModelSerializer):
status = serializers.CharField(max_length=30)
class Meta:
model = PowerStatus
class VMSerializer(serializers.ModelSerializer):
power_status = PowerStatusSerializer()
class Meta:
model = VirtualMachine
def create(self, validated_data):
power_status_data = validated_data.pop('power_status')
vm = VirtualMachine.objects.create(**validated_data)
PowerStatus.objects.create(vm=vm, **power_status_data)
return vm
Error that I'm getting is: django.db.utils.IntegrityError: (1048, "Column 'power_status_id' cannot be null")
I was following http://www.django-rest-framework.org/api-guide/serializers/#dealing-with-nested-objects but am doing something wrong.
:EDIT:
After dealing with Column cannot be null, another problem arose;
ValueError: Cannot assign "OrderedDict([('status', 'Running')])": "VirtualMachine.power_status" must be a "PowerStatus" instance.

In VirtualMachine model pass null=True to power_status field.
class VirtualMachine(models.Model):
power_status = models.ForeignKey(PowerStatus, verbose_name='Power status', null=True)

Related

how to set source for for a foreign key in serializer django rest frame work

i have model as follows:
class UserWorkspace(models.Model):
workspace = models.ForeignKey(
"Workspace", models.CASCADE, db_column="workspace_uuid"
)
user = models.ForeignKey("User", models.CASCADE, db_column="user_uuid")
and i need to change the name in serializer i tried:
class UserWorkspaceSerializer(serializers.ModelSerializer):
workspace_uuid = serializers.PrimaryKeyRelatedField(source="workspace", queryset=Workspace.objects.all())
user_uuid = serializers.PrimaryKeyRelatedField(source="user", queryset=User.objects.all())
class Meta:
model = UserWorkspace
fields = ("workspace_uuid", "user_uuid")
but i get the error
return self.fields[key]
KeyError: 'workspace'
change your code like this
workspace = serializers.PrimaryKeyRelatedField(source="workspace_uuid", queryset=Workspace.objects.all())
user = serializers.PrimaryKeyRelatedField(source="user_uuid", queryset=User.objects.all())
Source Documentation

How do you add existing objects to a ManyToMany field using Serializer?

I am trying to create an API with Artists and Songs, with a ManyToMany relationship between the two. Using the API to create a Song with an Artist that is not in the database works fine. The problem arises when I attempt to use the POST method to create a new Song with an Artist that already exists in the database. I tried overwriting the SongSerializer create() method using get_or_create() based on another post here, but I kept getting Bad Request errors when the Artist already exists in the database. The relevant code snippets:
models.py
class Artist(models.Model):
artist_name = models.CharField(max_length=200, unique=True)
class Meta:
ordering = ['artist_name']
def __str__(self):
return self.artist_name
class Song(models.Model):
song_title = models.CharField(max_length=200)
artists = models.ManyToManyField(Artist, related_name='songs')
class Meta:
ordering = ['song_title']
def __str__(self):
return self.song_title
serializers.py
class ArtistNameSerializer(serializers.ModelSerializer):
class Meta:
model = Artist
fields = ('artist_name',)
def to_representation(self, value):
return value.artist_name
class SongTitleSerializer(serializers.ModelSerializer):
songs = serializers.PrimaryKeyRelatedField(read_only=True, many=True)
def to_representation(self, value):
return value.song_title
class Meta:
model = Song
fields = ('songs',)
class ArtistSerializer(serializers.HyperlinkedModelSerializer):
songs = SongTitleSerializer(read_only=True, many=True)
class Meta:
model = Artist
fields = ('id', 'artist_name', 'songs')
class SongSerializer(serializers.HyperlinkedModelSerializer):
artists = ArtistNameSerializer(many=True)
class Meta:
model = Song
fields = ('id', 'song_title', 'artists',)
def create(self, validated_data):
artist_data = validated_data.pop('artists')
song = Song.objects.create(**validated_data)
song.save()
for artist_item in artist_data:
a, created = Artist.objects.get_or_create(artist_name=artist_item['artist_name'])
song.artists.add(a)
return song
I've done some tests and it looks like the program doesn't even go into the create() method I'm using, going straight to showing me the Bad Request error. What am I missing? Thanks in advance!
On you Artist model you have a constrain on the artist_model field (unique=True)
if you print the serializer in question with:
print(SongSerializer())
you get something like this:
SongSerializer():
id = IntegerField(label='ID', read_only=True)
song_title = CharField(max_length=200)
artists = ArtistNameSerializer(many=True):
artist_name = CharField(max_length=200, validators=[<UniqueValidator(queryset=Artist.objects.all())>])
under the artist_name field is a Validator "UniqueValidator"
so in case of a write operation you can disable the validator in the serializer with:
class ArtistNameSerializer(serializers.ModelSerializer):
class Meta:
model = models.Artist
fields = ('artist_name',)
extra_kwargs = {
'artist_name': {
'validators': [],
}
}
hope this help..

Django REST: Serializer lookup by UUID

I'm creating this simple shopping API in Django REST.
Internally I'm using IDs for foreign key constraints, while guuids are brought to the outside world.
For the checkout procedure, the user provides a list of article IDs he is willing to purchase. The object in the POST data thus looks as follows:
{
assets: [
{
'product': 'd9d5044d-2284-4d15-aa76-2eee3675035b',
'amount': 4
},
....
]
}
I'm using the following ticket/asset models:
# Ticket
class Ticket(models.Model):
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
owner = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='tickets', on_delete=models.CASCADE)
# Assets
class Asset(models.Model):
ticket = models.ForeignKey(Ticket, related_name='assets', on_delete=models.CASCADE)
stock_item = models.ForeignKey(Stock, related_name='stock_item', on_delete=models.SET_NULL, null=True)
amount = models.IntegerField(validators=[MinValueValidator(0)])
And the serializers look as follows:
# Asset serializer
class AssetSerializer(serializers.ModelSerializer):
class Meta:
model = Asset
fields = ('stock_item', 'amount')
# Ticket serializer
class TicketSerializer(WritableNestedModelSerializer):
owner = serializers.ReadOnlyField(source='owner.username')
assets = AssetSerializer(many=True)
class Meta:
model = Ticket
fields = ('uuid', 'owner', 'assets', )
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
When posting an object of the type specified above, the following error is presented:
{"assets":[{"stock_item": ["Invalid type. Expected PK, received string"]}]}
Which I can't seem to solve, how do I instruct the serializer to use the uuid as the lookup value? I solved a similar problem on view-level earlier by using the lookup_field member, but that doesn't seem to solve it. Any suggestions?
Enter code here
If I have understood you correctly, a SlugRelatedField should be able to find the correct related object.
class AssetSerializer(serializers.ModelSerializer):
ticket = serializers.SlugRelatedField(
read_only=True,
slug_field='uuid',
queryset=Ticket.objects.all() # Might be redundant with read_only=True
)
class Meta:
model = Asset
fields = ('ticket', 'stock_item', 'amount')
Elaborating on #BjornW's comment:
class UUIDRelatedField(serializers.SlugRelatedField):
slug_field = 'uuid'
def __init__(self, **kwargs):
super().__init__(slug_field=self.slug_field, **kwargs)
def to_representation(self, obj):
return getattr(obj, self.slug_field).hex

Django Rest Framework: Saving ForeignKey inside OneToOne model

I have 2 models that are OneToOne related and model that is FK to 2nd model
models.py
class Legal(TimeStampedModel):
name = models.CharField('Name', max_length=255, blank=True)
class LegalCard(TimeStampedModel):
legal = models.OneToOneField('Legal', related_name='legal_card', on_delete=models.CASCADE)
branch = models.ForeignKey('Branch', related_name='branch', null=True)
post_address = models.CharField('Post address', max_length=255, blank=True)
class Branch(TimeStampedModel):
name = models.CharField('Name',max_length=511)
code = models.CharField('Code', max_length=6)
Using DRF I made them to behave as single model so I can create or update both:
serializer.py
class LegalSerializer(serializers.ModelSerializer):
branch = serializers.IntegerField(source='legal_card.branch', allow_null=True, required=False)
post_address = serializers.CharField(source='legal_card.post_address', allow_blank=True, required=False)
class Meta:
model = Legal
fields = ('id',
'name',
'branch',
'post_address',
)
depth = 2
def create(self, validated_data):
legal_card_data = validated_data.pop('legal_card', None)
legal = super(LegalSerializer, self).create(validated_data)
self.update_or_create_legal_card(legal, legal_card_data)
return legal
def update(self, instance, validated_data):
legal_card_data = validated_data.pop('legal_card', None)
self.update_or_create_legal_card(instance, legal_card_data)
return super(LegalSerializer, self).update(instance, validated_data)
def update_or_create_legal_card(self, legal, legal_card_data):
LegalCard.objects.update_or_create(legal=legal, defaults=legal_card_data)
views.py
class LegalDetailView(generics.RetrieveUpdateDestroyAPIView):
queryset = Legal.objects.all()
serializer_class = LegalSerializer
I'm trying to save this by sending FK as integer (I just want to post id of the branch), but I receive error
ValueError: Cannot assign "2": "LegalCard.branch" must be a "Branch" instance.
Is there any way to pass over only ID of the branch?
Thank you
In Django, if you only need the FK value, you can use the FK value that is already on the object you've got rather than getting the related object.
Assume you have a Legal and Branch object with id's as 1. Then you can save a LegalCard object by:
LegalCard(legal_id=1,branch_id=1,post_address="Istanbul Street No:1")
Just use legal_card.branch_id instead of legal_card.branch to get just an id, not a related object.
And depth = 1

Django rest framework foreign key constraint fails creating model

I have the following in a Django Rest Framework setup:
models.py:
class Sku(BaseModel):
sku_code = models.CharField(max_length=18, primary_key=True)
supplier_id = models.PositiveIntegerField(db_index=True)
soh = models.PositiveIntegerField(default=0)
reserved = models.PositiveIntegerField(default=0)
broken = models.PositiveIntegerField(default=0)
unallocated = models.PositiveIntegerField(default=0)
reorder = models.PositiveIntegerField(default=0)
class Reservation(BaseModel):
sku = models.ForeignKey(Sku, db_column='sku_code')
order_id = models.PositiveIntegerField(db_index=True)
serializers.py:
class SkuSerializer(serializers.ModelSerializer):
class Meta:
model = Sku
fields = (
'sku_code',
'supplier_id',
'soh',
'reserved',
'broken',
'unallocated',
'reorder',
'created_at',
'modified_at',
)
class ReservationSerializer(serializers.ModelSerializer):
sku = SkuSerializer(read_only=True)
class Meta:
model = Reservation
fields = (
'id',
'order_id',
'sku',
'created_at',
'modified_at'
)
views.py:
class ReservationList(mixins.CreateModelMixin,
generics.GenericAPIView):
queryset = Reservation.objects.all()
serializer_class = ReservationSerializer
def post(self, request, *args, **kwargs):
sku = get_object_or_404(Sku, sku_code=request.data['sku_code'])
request.data['sku'] = sku
return self.create(request, *args, **kwargs)
Now when I post to the url linked to ReservationList.post view above I get the error: IntegrityError: (1048, "Column 'sku_code' cannot be null").
It seems to be bypassing the serializers validation and failing on the database layer. For some reason it doesn't accept the SKU being passed in.
What am I doing wrong here? I have tried to follow the example at http://www.django-rest-framework.org/api-guide/relations/#nested-relationships but this seems to break down with the CreateModelMixin. I can't tell if there is something wrong with how my models or serializers are set up.
You've set the sku field to read only, that's why the serializer is ignoring it when you post.
From the relevant documentation
Read-only fields are included in the API output, but should not be
included in the input during create or update operations. Any
'read_only' fields that are incorrectly included in the serializer
input will be ignored.

Categories

Resources