2

I'm studying djongo and i'm trying to create a platform that automatically assign a random amount (between 1 and 10) bitcoins to all new registered users.

My code is following:

#views.py
def register_page(request):
    if request.user.is_authenticated:
        return redirect('order_list')
    form = RegisterForm(request.POST)
    if form.is_valid():
        form.save()
        username = form.cleaned_data.get('username')
        messages.success(request,'Account successfully created, welcome '+ username)
        newUserProfile(username) #<------ this is the function to generate the profile with random BTC
        return redirect('login')
    context = {'form':form}
    return render(request, 'api/register.html', context)
from djongo import models
from django.contrib.auth.models import User

#models.py
class UserProfile(models.Model):
    id = models.BigAutoField(primary_key=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    BTC = models.FloatField()
    balance = models.FloatField()
    pending_balance = models.FloatField()
    pending_BTC = models.FloatField()
#utils.py
def newUserProfile(username):
    user = User.objects.get(username=username)
    BTC = round(random.uniform(1,10),2)
    profile = UserProfile.objects.create(user=user, BTC=BTC, balance = 0, pending_balance = 0, pending_BTC = 0)
    profile.save()

When i push the register button on my webpage i get:

Exception Type: TypeError
Exception Value:    
Field 'id' expected a number but got ObjectId('606d892cb5d1f464cb7d2050').

but when i go in the database the new profile is regularly recorded:

# userprofile tab
{"_id":{"$oid":"606d892cb5d1f464cb7d2050"},
"user_id":49,
"BTC":3.26,
"balance":0,
"pending_balance":0,
"pending_BTC":0}

# auth_user tab
{"_id":{"$oid":"606d892cb5d1f464cb7d204f"},
"id":49,
"password":"pbkdf2_sha256$180000$nNwVYtrtPYj0$/wwjhAJk7zUVSj8dFg+tbTE1C1Hnme+zfUbmtH6V/PE=",
"last_login":null,
"is_superuser":false,
"username":"Aokami",
"first_name":"",
"last_name":"",
"email":"Aokami@gmail.com",
"is_staff":false,
"is_active":true,
"date_joined":{"$date":"2021-04-07T10:27:56.590Z"}}

How to resolve this, or atleast avoid the error page since i obtained anyway what i needed?

Aokami
  • 35
  • 1
  • 6

2 Answers2

2

An _id field is added automatically to an object by django. You've added another field called id, which is of type number. Hence, you can't assign the value of django's _id to your id field because its not of the same type. See how you can customize the django's id field in order to avoid this duplication -> Automatic primary key fields.

You can use this line from django's docs id = models.BigAutoField(primary_key=True)

Tomer
  • 1,521
  • 1
  • 15
  • 26
  • Hi Tomer! thank you for your answer, I've just applied this and it works better than before in db's but it still give me the same error on the webpage – Aokami Apr 07 '21 at 11:10
  • If you’d like to specify `id` as a primary key use primary_key=True on one of your fields. If Django sees you’ve explicitly set Field. primary_key, it won’t add the automatic id column. Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added). – Tomer Apr 07 '21 at 11:15
  • Same problem on the webpage, i also edited the first post with this new line and new db's output :) – Aokami Apr 07 '21 at 12:56
  • `id = models.BigAutoField(primary_key=True)` should be in the User model not UserProfile model .. – Tomer Apr 07 '21 at 12:59
0

Correct answer is in the comments. I had the same problem. You have to use DJONGO models instead of django models.

Just use from djongo import models instead of the import from django. It`s easy to overread this.

user1383029
  • 1,685
  • 2
  • 19
  • 37