I want to store the data acquired on the server side with django in the database, but I get an error saying ValueError: Field'id' expected a number but got'suzukitadashi'.
In models.py in the app project
from django.db import models from django.contrib.auth.models import User class User (models.Model): user_id = models.CharField (max_length = 10) password1 = models.CharField (max_length = 20) password2 = models.CharField (max_length = 20) class Analytics (models.Model): user_foreign_key = models.ForeignKey ("auth.User", on_delete = models.CASCADE, verbose_name = "foreign_key") point = models.CharField (max_length = 200)
In models.py in the index project
from django.db import models class UserData (models.Model): data_foreign_key = models.ForeignKey ("registration.User", on_delete = models.CASCADE) user_id = models.CharField (max_length = 20) access_date = models.DateTimeField ('access date and time')
def top (request): json_data = json.loads (request.body.decode ("utf-8"), strict = False) user_data = UserData.objects.get (data_foreign_key = "suzukitadashi") user_data.user_id = get_random_string (10) user_data.access_date = datetime.datetime.now () user_data.save () return JsonResponse (json_data, safe = False)
When I accessed this top method and executed the data storage code, I got the above error.
As a test
user_data = UserData.objects.get (data_foreign_key = 1)
I got an error even if I rewrote it.
I'm logged in to the django app with suzukitadashi.
What is the error causing the problem? ??
Answer # 1
I'm sorry. I didn't know where to fix it, so I corrected it.
"Value Error: Cannot query" suzukitadashi ": Must be" User "instance." Indicates that "" suzukitadashi "cannot be queried, so it must be a" User "instance".
def top (request): json_data = json.loads (request.body.decode ("utf-8"), strict = False) # user_data = UserData.objects.get (data_foreign_key = "suzukitadashi") user = User.objects.get (user_id ='suzukitadashi') user_data = UserData.objects.get (data_foreign_key = user) user_data.user_id = get_random_string (10) user_data.access_date = datetime.datetime.now () user_data.save () return JsonResponse (json_data, safe = False)
- python - when i set the uuid field in django, i get field'object_id' expected a number but got '78a0767cb607488087a73f17565bb
- where is the minor version number of python that starts by default specified?
- python sarimax valueerror: maxlag should be <what causes nobs?
- python - when you want to judge by the number of characters from the back with a regular expression
- python - how to get the number of searches for a specific word within the period
- python - sumy by specifying the maximum number of characters with sumy
- python - i want to add a line number to the data frame
- number of python combinations
- python 3x - how to count the number of data extracted by pandas or how to solve the error
- python - how to accept number input even if keypressevent is defined for qlineedit in pyside
- python - i want to divide a binary number into two, store it in a list, and represent it in a different decimal number
- i want to invert the number 12345 in python like 54321 and display it
- python - valueerror: error when checking input: expected input_1 to have 4 dimensions,
- get the number of characters from the python text box
- get the total number and name of python grouping
- how to align the number of elements with overlaplengt of python etc
- set python excel serial number to date and time
- how to multiply each element by a number in a python 2d list
- python 3x - i want to output the name corresponding to the number entered in the excel file
- Python and Java. Combination
- python : Do not save fields in the form of Django
- python : Redirect /on the page without a layer Django
- python : It is necessary that the "Delete" Django button appears next to each comment.
- python : Models.cascade need help! Cleared one table, and with it the other cleared. Django 3.x.
- python : Not transmitted input data in Django Form
- python : Gives an error Page not Found in Django
- python : ATTRIBUTEERROR: 'AnonymousUser' Object Has No Attribute '_meta' User Model Supplement
- python : Errors when starting Django project