Answer the question
In order to leave comments, you need to log in
How to fix error when writing to DB (linked tables)?
Good afternoon!
Please tell me what I'm doing wrong.
Project on python 3.6 + django 2.0 + rest_framework.
The database is sqlite3.
There are 2 related models in the project (respectively 2 related tables in the database): Event (meetings) + Place (places where the meetings will take place).
The Event table has a place field - it must contain the id from the Place table.
Code from models.py file:
class Place(models.Model):
title_p = models.CharField(max_length=100)
type_p = models.CharField(max_length=100)
adress_p = models.CharField(max_length=200)
link_p = models.CharField(max_length=255)
class Meta:
ordering = ('title_p',)
class Event(models.Model):
title = models.CharField(max_length=100)
description = models.CharField(max_length=200)
price = models.CharField(max_length=100)
place = models.ForeignKey(Place, on_delete=models.PROTECT)
link = models.CharField(max_length=255)
tags = models.CharField(max_length=200)
date = models.DateField()
time = models.TimeField()
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ('created_at',)
class PlaceSerializer(serializers.ModelSerializer):
class Meta:
model = Place
fields = ('id', 'title_p', 'type_p', 'adress_p', 'link_p')
class EventSerializer(serializers.ModelSerializer):
place = PlaceSerializer(read_only=True)
class Meta:
model = Event
fields = ('id', 'title', 'description', 'price', 'place', 'link', 'date', 'time')
HTTP 200 OK
Allow: GET, PUT, PATCH, DELETE, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
"id": 1,
"title": "Встреча1",
"description": "Описание1",
"price": "100",
"place": {
"id": 1,
"title_p": "Место1",
"type_p": "ТипМеста1",
"adress_p": "Адрес1",
"link_p": "http://vk.com/111/"
},
"link": "http://vk.com/11111/",
"date": null,
"time": "23:00:00"
}
http -a admin:???????? POST http://localhost:8000/events/ title=Встреча2 description=Описание2 price=200 place=1 link=http://vk.com/22222/ date=2018-01-23 time=23:00
Exception Type: IntegrityError at /events/
Exception Value: NOT NULL constraint failed: events_event.place_id
Answer the question
In order to leave comments, you need to log in
Unfortunately it didn't work, same error:
class EventGETSerializer(serializers.ModelSerializer):
place = PlaceSerializer(read_only=True)
class Meta:
model = Event
fields = ('id', 'title', 'description', 'price', 'place', 'link', 'date', 'time')
class EventSerializer(serializers.ModelSerializer):
class Meta:
model = Event
fields = ('id', 'title', 'description', 'price', 'place', 'link', 'date', 'time')
from rest_framework import permissions
class EventList(generics.ListCreateAPIView):
queryset = Event.objects.all()
def get_serializer_class(self):
if self.request.method in permissions.SAFE_METHODS:
return EventGETSerializer
else:
return EventSerializer
from rest_framework.permissions import SAFE_METHODS
class EventSerializer(serializers.ModelSerializer):
def __init__(self, *args, **kwargs):
request = kwargs.get('context', {}).get('request')
if request is not None and request.method not in SAFE_METHODS:
self.Meta.depth = 0
else:
self.Meta.depth = 1
super().__init__(*args, **kwargs)
class Meta:
model = Event
fields = ('id', 'title', 'description', 'price', 'place', 'link',
'date', 'time')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question