djongo icon indicating copy to clipboard operation
djongo copied to clipboard

Djongo + mongoDB get ObjectId

Open ghost opened this issue 5 years ago • 2 comments

Hello there, dataclasses 0.6 Django 2.2.5 djangorestframework 3.10.2 djongo 1.2.35 pip 19.0.3 pymongo 3.9.0 pytz 2019.2 setuptools 40.8.0 sqlparse 0.2.4

In djongo, I want to get the "_id" field on mongoDB with the rest framework. can you help me ? I want to get the ObjectId field

<#from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
import uuid
from djongo import models


class Customer(models.Model):
    # musteri hangi kullaniciya ait oldugunu ogrenmek icin
    user = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
    _id = models.ObjectIdField()
    customerName = models.CharField(max_length=40)
    customerSurname = models.CharField(max_length=40)
    customerIdentityNo = models.CharField(max_length=12)  # , unique=True)
    customerGender = models.CharField(max_length=1)  # E = Erkek, K = Kadın
    customerPhone = models.CharField(max_length=12)
    customerBirth = models.TextField()
    customerDescription = models.TextField()
    customerStatus = models.BooleanField(default=False)
    created = models.DateTimeField(editable=False)
    modified = models.DateTimeField()
    modified_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='modifiedBy')
    parent_user = models.TextField(editable=False)
    child_user = models.TextField(editable=False)

    def __str__(self):
        return self.customerName

    # customer ilk defa olusturulduysa burasi calisir
    def save(self, *args, **kwargs):
        if not self.id:
            self.created = timezone.now()

        self.modified = timezone.now()
        return super(Customer, self).save(*args, **kwargs)
>



**serializer.py**
<from rest_framework import serializers
from customer.models import Customer


class CustomerSerializer(serializers.ModelSerializer):
    username = serializers.SerializerMethodField(method_name='username_new')

    # _id = ObjectIdField(source='_id')

    class Meta:
        model = Customer
        fields = [
            '_id',
            'username',
            'customerName',
            'customerSurname',
            'customerIdentityNo',
            'customerGender',
            'customerPhone',
            'customerBirth',
            'customerDescription',
            'customerStatus',
            'created',
            'modified',
            'modified_by'
        ]

    def username_new(self, obj):
        return str(obj.user.username)

    def update(self, instance, validated_data):
        instance.customerName = validated_data.get('customerName', instance.customerIdentityNo)
        instance.customerSurname = validated_data.get('customerSurname', instance.customerIdentityNo)
        instance.customerIdentityNo = validated_data.get('customerIdentityNo', instance.customerIdentityNo)
        instance.customerGender = validated_data.get('customerGender', instance.customerIdentityNo)
        instance.customerBirth = validated_data.get('customerBirth', instance.customerIdentityNo)
        instance.customerDescription = validated_data.get('customerDescription', instance.customerIdentityNo)
        instance.save()
        return instance

    def create(self, validated_data):
        # print(validated_data)
        return Customer.objects.create(user=self.context["request"].user, **validated_data)
>

ghost avatar Sep 02 '19 19:09 ghost

Hello: the discussion on issue #298 should help solve your problem. Let me know if that doesn't help: hopefully we can work through it

SomeoneInParticular avatar Sep 06 '19 20:09 SomeoneInParticular

set the fields='__all__

RayyanNafees avatar Jul 30 '21 05:07 RayyanNafees