unable to access object's property despite it being present

My serializer: class TaskSerializer(serializers.ModelSerializer): class Meta: model = Task fields = "__all__" def create(self, validated_data): try: print(validated_data) print(validated_data.author) task = Task.objects.create(**validated_data) return task except BaseException as e: print(e) raise HTTP_400_BAD_REQUEST my view: class TaskCreateApiView(generics.CreateAPIView): serializer_class = TaskSerializer my model: from django.db import models from django.contrib.auth.models import User class Task(models.Model): content = models.CharField( default="", max_length=255, )… Read More unable to access object's property despite it being present

django.db.utils.IntegrityError: UNIQUE constraint failed: accounts_profile.user_id

I’m having issues with registering a user in my project. I have a custom serializer that inherits from the RegisterView of dj_rest_auth. My code is: views.py class ProfileDetail(APIView): def get_object(self, pk): try: return Profile.objects.get(pk=pk) except Profile.DoesNotExist: raise Http404 def get(self, request, pk, format=None): profile = self.get_object(pk) serializer = ProfileSerializer(profile) return Response(serializer.data) class CustomRegisterView(RegisterView): serializer_class =… Read More django.db.utils.IntegrityError: UNIQUE constraint failed: accounts_profile.user_id

array is not acting like an array and i don't know what to do

I am making a react application with django backend. So for protected routes i am doing something like this. <Route element={<RequireAuth allowedRoles={[‘admin’]}/>}> <Route path=’admin’ element={<Admin />} /> </Route> And in RequireAuth.js I have this: const RequireAuth = (allowedRoles) => { const location = useLocation() const userdata= JSON.parse(localStorage.getItem(‘user’)) || {} return ( (allowedRoles.includes(userdata.user.role)) ? <Outlet />… Read More array is not acting like an array and i don't know what to do

How to show name instead of id with foreighkey with django restframework

I have a django application. And a foreign relationship. But the api calls shows the id instead of the name. But I want to show the name instead of the id. So this is the property: category = models.ForeignKey(Category, related_name=’animals’, on_delete=models.CASCADE, verbose_name="test") and serializer: class SubAnimalSerializer(serializers.ModelSerializer): class Meta: model = Animal fields = [‘id’, ‘name’,… Read More How to show name instead of id with foreighkey with django restframework

Field is required, when it's not defined as so

I have the following Django model class Component(models.Model): parent = models.ForeignKey( "self", on_delete=models.CASCADE, null=True, blank=True, related_name="children" ) vessel = models.ForeignKey( Vessel, on_delete=models.CASCADE, related_name="components" ) name = models.CharField(max_length=100) manufacturer = models.CharField(max_length=255, null=True, blank=True) model = models.CharField(max_length=255, null=True, blank=True) type = models.CharField(max_length=255, null=True, blank=True) serial_number = models.CharField(max_length=255, null=True, blank=True) supplier = models.CharField(max_length=255, null=True, blank=True) description = models.TextField(null=True,… Read More Field is required, when it's not defined as so

calling a url with reverse with query string during unit testing

I have this URL that i want to test: urlpatterns = [ path(‘produce<str:code>’, ProduceSpreadSheet.as_view(), name="produce" ), ] my test is: class ProduceSpreadSheetTest(TestCase): def setUp(self): self.code = "abcd" def test_valid_token(self): url = reverse(‘produce’, query_kwargs={‘code’: self.code}) response = client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) I get the error: TypeError: reverse() got an unexpected keyword argument ‘query_kwargs’ when I change query_kwargs… Read More calling a url with reverse with query string during unit testing

Populate parent field from url kwarg in a nested serializer in Django REST Framework

I have 2 models, Catalog and Epic: class Catalog(models.Model): created_on = models.DateTimeField(auto_now_add=True) active = models.BooleanField(null=False, default=False) class Epic(models.Model): name = models.CharField(max_length=128, null=False) slug = models.SlugField(null=False) catalog = models.ForeignKey(Catalog, null=False, on_delete=models.CASCADE) I have created CRUD viewset and serializer for Catalog, using DRF. Now I want to create CRUD viewset and serializer for Epic, to be used… Read More Populate parent field from url kwarg in a nested serializer in Django REST Framework