Поле Django ForeignKey("x") должно быть экземпляром "x" при обновлении [закрыто]
Django версии 4.2.9
Я пытаюсь исправить это : Невозможно присвоить "('customer',)": "Project.customer" должен быть экземпляром "Customer".
У меня есть 2 класса, Project и Customer.
class Customer(models.Model):
customer_name = models.CharField(max_length=50)
string = models.CharField(max_length=50)
def __str__(self):
return f"{self.customer_name}"
class Project(models.Model):
project_name = models.TextField()
project_code = models.CharField(max_length=50,null=True,blank=True)
project_scope = models.CharField(max_length=200,blank=True,null=True)
customer = models.ForeignKey("Customer", on_delete=models.DO_NOTHING)
project_type = models.ForeignKey("ProjectType", on_delete=models.DO_NOTHING)
is_rd = models.BooleanField(null=True,blank=True)
#TARIHLER
request_date = models.DateField(auto_now=False, auto_now_add=False)
confirmation_date = models.DateField(auto_now=False, auto_now_add=False, null=True,blank=True)
estimated_finish_date = models.DateField(auto_now=False, auto_now_add=False)
project_finish_date = models.DateField(auto_now=False, auto_now_add=False, null=True,blank=True,default="01/01/1970")
update_date = models.DateField(auto_now=False, auto_now_add=False,null=True, blank=True,default="01/01/1970")
start_date = models.DateField(auto_now=False, auto_now_add=False,null=True, blank=True,default="01/01/1970")
#TARIHLER
update_count = models.IntegerField(null=True,blank=True)
estimated_cost = models.IntegerField()
actual_cost = models.IntegerField(null=True,blank=True)
is_active = models.BooleanField(default=False)
project_leader_id = models.ForeignKey("account.MamurUser", on_delete=models.DO_NOTHING)
bill = models.BooleanField()
patent = models.BooleanField(default=False,blank=True)
market = models.BooleanField(default=False,blank=True)
is_confirmed = models.BooleanField(default=False,blank=True)
is_pmo_confirmed = models.BooleanField(default=False,blank=True)
Вот мой views.py:
def edit_project(request):
context={
"projects" : Project.objects.filter(is_confirmed=False)
}
if request.method == "GET":
return render(request,'pyka/project-edit.html',context)
else:
post = request.POST
project_id = post['project_id']
project = Project.objects.get(id=project_id)
update_count = project.update_count
update_count += 1
valid = True # değişecek
error = None
now = datetime.date.today()
project_name = post['project-name']
customer = Customer.objects.get(id=post['customer'])
type = ProjectType.objects.get(id=post['type'])
project_scope = post['project-scope']
project_budget = post['project-budget']
user_data = post['userData']
user_data = json.loads(user_data)
start_date = post['start-date']
end_date = post['end-date']
patent = post.get('patent', False)
market = post.get('market', False)
# VALIDATION
if valid:
pd = ProjectDetail.objects.filter(project_id=project_id)
for d in pd:
if d.project_role is not Role.objects.get(id=2):
d.delete()
project.project_name = project_name,
project.project_scope = project_scope,
project.customer = customer,
project.project_type = type,
project.estimated_finish_date = end_date,
project.update_date = now,
project.update_count = update_count,
project.start_date = start_date,
project.estimated_cost = project_budget,
project.patent = patent,
project.market = market,
project.save()
users = MamurUser.objects.all()
pmo = Role.objects.filter(id=4).first()
# Tüm PMO Userları projeye dahil et
for user in users:
if user.role_id == pmo:
ProjectDetail.objects.create(
user_id = MamurUser.objects.filter(id=user.id).first(),
project_id = project,
project_role = pmo,
)
ProjectDetail.objects.create(
user_id = MamurUser.objects.filter(id=request.user.id).first(),
project_id = project,
project_role = Role.objects.filter(id=2).first(),
)
for data in user_data:
user_id = data.get('user')
ProjectDetail.objects.create(
user_id = MamurUser.objects.filter(id=user_id).first(),
project_id = project,
project_role = Role.objects.filter(id=1).first(),
)
context += {"success","Başarıyla Güncellendi"}
return render(request,'pyka/project-edit.html',context)
и бросает вот это :
Внутренняя ошибка сервера: /edit-project raise ValueError( ValueError: Невозможно присвоить "('customer',)": "Project.customer" должен быть экземпляром "Customer".
Я пытаюсь обновить экземпляр проекта, но Django выбрасывает исключение