I have Django 2 that the Rest Framework and Djongo is used in it.
The Account can be created but when I try to see the list of created Accounts to updated or do any operation on them I can't actually I can't retrieve the list of Accounts and gives me error on ID field.
I think this is an issue with MongoDB, so how I can fix this issue?
Here is the error that I have that followed by my Account related codes
MigrationError at /Accounts/account/ id Request Method: GET Request URL: http://localhost:8000/Accounts/account/ Django Version: 2.2.7 Exception Type: MigrationError Exception Value:
id Exception Location: C:\Users\PC 001\AppData\Local\Programs\Python\Python38-32\lib\site-packages\djongo\sql2mongo\query.py in _align_results, line 287 Python Executable: C:\Users\PC 001\AppData\Local\Programs\Python\Python38-32\python.exe Python Version: 3.8.0 Python Path:
['E:\Project\accountingPy', 'C:\Users\PC ' '001\AppData\Local\Programs\Python\Python38-32\python38.zip', 'C:\Users\PC 001\AppData\Local\Programs\Python\Python38-32\DLLs', 'C:\Users\PC 001\AppData\Local\Programs\Python\Python38-32\lib', 'C:\Users\PC 001\AppData\Local\Programs\Python\Python38-32', 'C:\Users\PC 001\AppData\Roaming\Python\Python38\site-packages', 'C:\Users\PC ' '001\AppData\Local\Programs\Python\Python38-32\lib\site-packages'] Server time: Mon, 6 Apr 2020 15:00:38 +0000
The record that stored in database
// 1
{
"_id": ObjectId("5e8b3a0c10fc9f40cdca44be"),
"label": "Mapping",
"owner": "Nasser",
"balance_currency": "USD",
"balance": "23.00",
"desc": "This the Cash account that manage by cash payments",
"status": true,
"created_at": ISODate("2020-04-06T14:17:48.838Z"),
"updated_at": ISODate("2020-04-06T14:17:48.838Z")
}
Account model
class Account(models.Model):
class Meta:
db_table = 'account'
def __str__(self):
return self.label
label = models.CharField(max_length=20,unique=True)
owner = models.CharField(max_length=20)
balance = MoneyField(max_digits=14, decimal_places=2,
default_currency='USD')
desc = models.CharField(max_length=200, blank=True)
status = models.BooleanField(default=1)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
Account Serializer
class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = ['label', 'owner', 'balance',
'desc', 'status', 'created_at', 'updated_at']
Urls
router = DefaultRouter()
router.register(r'', AccountViewSet, basename='account')
urlpatterns = router.urls
Account View Set
class AccountViewSet(viewsets.ViewSet):
def list(self, request):
account = Account.objects.all()
serializer = AccountSerializer(account, many=True)
return JsonResponse(serializer.data, safe=False)
# return HttpResponse("List")
AccountFrom
class AccountForm(ModelForm):
class Meta:
model = Account
fields = ['label', 'owner', 'balance',
'desc', 'status']
Basic Solution I add the id field none autoIncrement to the models and the issue has been fixed.
id = models.IntegerField(primary_key=True, unique=True)
But I think its not good solution as the Id is not Auto Increment and each time I need to check the last Id to have an Auto Increment ID field. How I can create auto Increment field in MongoDB?