django-rest-framework

DRF ManyToMany Field getting an error when creating object

DRF ManyToMany Field getting an error when creating object Question: I have a Rant model with Category linked to it using ManyToManyField. I’ve serialized it but the problem is this error: { "categories": [ "Expected a list of items but got type "str"." ] } These are my serializers: class CategorySerializer(serializers.ModelSerializer): class Meta: model = …

Total answers: 3

Django ORM & Django REST Framework: How to make "grouped" arrays?

Django ORM & Django REST Framework: How to make "grouped" arrays? Question: Currently, my Django server can return the JSON below: [ { "id": 1, "customer": { "id": 1, "name": "John Doe" }, "description1": "…", "description2": "…", "description3": "…", "description4": "…" }, { "id": 2, "customer": { "id": 1, "name": "John Doe" }, "description1": "…", …

Total answers: 2

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

Populate parent field from url kwarg in a nested serializer in Django REST Framework Question: 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 …

Total answers: 1

NOT NULL constraint failed null=True blank=True

NOT NULL constraint failed null=True blank=True Question: despite having null and blank true in my models I always get NOT NULL constraint failed: user_mgmt_profile.user_id I’ve deleted the database, deleted migrations, run commands again, and the error still persist. Where do you see the issue Basically Im automatically connecting user to new profile on post request …

Total answers: 1

Populate readonly nested serializer

Populate readonly nested serializer Question: I’m using ModelViewSets and I have nested serializer for these models. class Profile(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) user = models.OneToOneField(User, on_delete=models.CASCADE) place = models.OneToOneField(Place, on_delete=models.DO_NOTHING) bio = models.TextField(null=True) class User(AbstractBaseUser, PermissionsMixin): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) email = models.EmailField(_(’email address’), unique=True) first_name = models.CharField(max_length=150, blank=True) is_staff = models.BooleanField(default=False) is_active …

Total answers: 2

Python django auth test

Python django auth test Question: I want to write tests for my python django application. For tests, I need to get an API token, I tried it through APIClient with this code: client = APIClient() responce = json.loads(client.post(‘/api-token-auth/’,data={"username":"root", "password":"root"}).content) But in return I get {‘non_field_errors’: [‘Unable to login with provided credentials.’]} Through the "requests" library …

Total answers: 1

nothing happens after running python manage.py shell < script.py

nothing happens after running python manage.py shell < script.py Question: This is my first time trying to run a script. I’m trying to hash previously stored plain text in my db. Anyway I created script.py file in my project folder it doesn’t seem to do anything what am I missing? The command I’m running in …

Total answers: 1

Filter by time in DRF

Filter by time in DRF Question: I have an api with an end day field (ad end time), in the model, this is a simple datetime field: end_date = DateTimeField(null=True, blank=True) in my view there are several types of filtering for ads, but I need some checkbox that will compare the current date and time …

Total answers: 1