Using Django and its REST_Framework, I'm trying to add an object to my POSTGRES-11 database, whose model (Team) is related to another model (Country) by a ForeignKey relation. The JSON I send with the post request looks like this, where the number provided for country is supposed to be its id.
name:"test"
is_onsite:true
status:"PAID"
institution:"testU"
country:2
But then Django returns the following error:
IntegrityError at /api/register/team/
null value in column "country_id" violates not-null constraint
DETAIL: Failing row contains (28, , f, PENDING, , null).
I've tried sending the same json with 'country' replaced with 'country_id', but I've been faced with the same error.
I've already tried the solution given Here, but then Django returns this JSON instead of adding the object:
{
"country": [
"This field is required."
]
}
models.py:
class Country(models.Model):
name = models.CharField(max_length=255)
flag = models.ImageField()
class Meta:
verbose_name_plural = 'Countries'
def __str__(self):
return self.name
class Team(models.Model):
name = models.CharField(max_length=255, default="", blank=True)
is_onsite = models.BooleanField(default=False)
status = models.CharField(max_length=50, choices=TEAM_STATUS_CHOICES, default='PENDING')
institution = models.CharField(max_length=255, default="")
country = models.ForeignKey(Country, on_delete=models.CASCADE, default="", blank=True)
def __str__(self):
return self.name
serializers.py:
class CountrySerializer(serializers.ModelSerializer):
class Meta:
model = Country
fields = '__all__'
class TeamSerializer(serializers.ModelSerializer):
class Meta:
model = Team
fields = ['name', 'is_onsite', 'status', 'institution', 'country']
views.py:
class TeamCreateView(CreateAPIView):
queryset = Team.objects.all()
serializer_class = TeamSerializer
I should note that Django works perfectly fine when I try to add it via the HTML form provided at '/api/register/team', but not when it receives a JSON via outer post requests.
I've been stuck at this for days, so your help would be really appreciated.
@dirkgroten I forgot to mention, I did the former thing by adding the following snippet to my TeamSerializer and tried sending the 'countryId' (and not 'country_id') in my POST request, but it still didn't work. This snippet:
countryId = serializers.PrimaryKeyRelatedField(queryset=Country.objects.all())
def create(self, validated_data):
print(validated_data)
return Team.objects.create(
name=validated_data['name'],
is_onsite=validated_data['is_onsite'],
status=validated_data['status'],
institution=validated_data['institution'],
country=validated_data['countryId']
)
As for the latter case, I added the
country = CountrySerializer(many=False)
line to my code, and sent a JSON like the following, but I still got the "This field is required" JSON.
My POST JSON:
name:"test"
is_onsite:true
status:"PAID"
institution:"testU"
country:{"name": "Test"}
Even though the country 'Test' exists in my database, it still fails to add the team.