3

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?

Nasser Ali Karimi
  • 4,462
  • 6
  • 34
  • 77
  • I fixed the issue with removing database and regenerate the migrations, in new database that created the ID field is added beside the _id and is auto increment, This is not good solution if I had any important data on database, but works for me as I just developing from Scratch. – Nasser Ali Karimi Apr 06 '20 at 17:46
  • 1
    True, dropping table and recreating them worked for me. But, there must be some other way too. Don't know why this happens even. I had this issue twice. – Eranki May 15 '21 at 12:59

1 Answers1

0

What I did was running python manage.py <app_name> zero wich removes all migrations. Then I manually deleted migrations files from migrations folder and run python manage.py makemigrations + python manage.py migrate.

Solved it for me.