“queryset”对象没有属性“save”错误

knpiaxh1  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(542)

我是一名学习django的学生。目前,成员信息已输入到成员表中。我想创建mypage,以便修改在成员表中注册的成员信息,并开发mypage,以便在mypage上修改成员信息。但在开发过程中,我遇到了以下错误:我如何解决这个问题?任何帮助都将不胜感激。
详细信息错误:

AttributeError at /mypage/mypage_modify/
'QuerySet' object has no attribute 'save'
Request Method: POST
Request URL:    http://127.0.0.1:8000/mypage/mypage_modify/
Django Version: 3.1.5
Exception Type: AttributeError
Exception Value:    
'QuerySet' object has no attribute 'save'
Exception Location: C:\zeronine_project\mypage\views.py, line 27, in mypage_modify
Python Executable:  D:\anaconda3\envs\vnv_zn\python.exe
Python Version: 3.7.6
Python Path:    
['C:\\zeronine_project',
 'D:\\anaconda3\\envs\\vnv_zn\\python37.zip',
 'D:\\anaconda3\\envs\\vnv_zn\\DLLs',
 'D:\\anaconda3\\envs\\vnv_zn\\lib',
 'D:\\anaconda3\\envs\\vnv_zn',
 'D:\\anaconda3\\envs\\vnv_zn\\lib\\site-packages']
Server time:    Thu, 08 Jul 2021 21:05:53 +0900

view.py

from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect

from mypage.forms import *
from zeronine.models import *

# Create your views here.

def mypage_list(request):
    categories = Category.objects.all()

    return render(request, 'mypage/mypage_list.html', {'categories':categories})

def mypage_modify(request):
    current_category = None
    categories = Category.objects.all()
    member = Member.objects.all()

    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse('zeronine:login'))

    if request.method == "POST":
        member.name = request.POST['name']
        member.password = request.POST['password']
        member.username = request.user
        member.save()
        return redirect('zeronine:post')

    else:
        memberForm = MemberForm
        return render(request, 'mypage/mypage_modify.html', {'memberForm':memberForm, 'member':member, 'current_category': current_category, 'categories': categories})

models.py

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.urls import reverse

# 회원

class Member(AbstractUser):
    username = models.CharField(primary_key=True, max_length=20, verbose_name='아이디')
    name = models.CharField(max_length=20, verbose_name='이름', default='')
    password = models.CharField(max_length=64, verbose_name='비밀번호')
    phone = models.CharField(max_length=64, verbose_name='전화번호')

    def __str__(self):
        return self.username

    class Meta:
        verbose_name = ('Member')
        verbose_name_plural = ('Members')

# 카테고리

class Category(models.Model):
    category_code = models.AutoField(primary_key=True)
    name = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField(max_length=200, db_index=True, allow_unicode=True)

    class Meta:
        ordering =['category_code']
        verbose_name = 'category'
        verbose_name_plural = 'categories'

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('zeronine:product_in_category', args=[self.slug])

# 상품

class Product(models.Model):
    product_code = models.AutoField(primary_key=True)
    username = models.ForeignKey(Member, on_delete=models.CASCADE, db_column='username')
    category_code = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, related_name='products')
    name = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField(max_length=200, db_index=True, unique=False, allow_unicode=True)
    image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
    benefit = models.TextField()
    detail = models.TextField()
    target_price = models.IntegerField()
    start_date = models.DateField()
    due_date = models.DateField()

    class Meta:
        ordering = ['product_code']
        index_together = [['product_code', 'slug']]

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('zeronine:product_detail', args=[self.product_code, self.slug])

class Post(models.Model):
    post_code = models.AutoField(primary_key=True)
    username = models.ForeignKey(Member, on_delete=models.CASCADE, db_column='username')
    title = models.CharField(max_length=64)
    content = models.TextField()
    register_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ['register_date']

# 댓글(댓글코드, 게시글코드(fk), 아이디(fk), 내용, 등록날짜)

class Comment(models.Model):
    comment_code = models.AutoField(primary_key=True)
    post_code = models.ForeignKey(Post, on_delete=models.CASCADE, db_column='post_code')
    username = models.ForeignKey(Member, on_delete=models.CASCADE, db_column='username')
    content = models.TextField()
    register_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.content

    class Meta:
        ordering = ['register_date']

# Zzim 모델부터 새로 추가하려는 모델들 입니다.

# 찜하기(찜코드, 아이디(fk), 상품코드(fk))

class Zzim(models.Model):
    zzim_code = models.AutoField(primary_key=True)
    username = models.ForeignKey(Member, on_delete=models.CASCADE, db_column='username')
    product_code = models.ForeignKey(Product, on_delete=models.CASCADE, db_column='product_code')

    def __str__(self):
        return str(self.zzim_code)

# 상품이미지(이미지코드, 상품코드(fk), 이미지경로)

class Photo(models.Model):
    photo_code = models.AutoField(primary_key=True)
    product_code = models.ForeignKey(Product, on_delete=models.CASCADE, db_column='product_code')
    photo = models.ImageField(upload_to="photos/%Y%m%d")

    def __str__(self):
        return str(self.photo_code)

    class Meta:
        ordering = ['photo_code']

# 옵션(옵션코드, 옵션명, 상품코드(fk))

class Option(models.Model):
    option_code = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32)
    product_code = models.ForeignKey(Product, on_delete=models.CASCADE, db_column='product_code')

    def __str__(self):
        return self.name

# 옵션값(옵션값코드, 옵션값명, 옵션코드(fk), 상품코드(fk))

class Value(models.Model):
    value_code = models.AutoField(primary_key=True)
    option_code = models.ForeignKey(Option, on_delete=models.CASCADE, db_column='option_code')
    product_code = models.ForeignKey(Product, on_delete=models.CASCADE, db_column='product_code')
    name = models.CharField(max_length=32)

    def __str__(self):
        return self.name

# 옵션지정상품(옵션지정상품코드, 상품코드(fk), 가격, 참여수량)

class Designated(models.Model):
    designated_code = models.AutoField(primary_key=True)
    product_code = models.ForeignKey(Product, on_delete=models.CASCADE, db_column='product_code')
    price = models.IntegerField()
    rep_price = models.BooleanField(default=True)

    class Meta:
        ordering = ['designated_code']

    def __str__(self):
        return str(self.designated_code)

# 상품옵션구성(상품옵션구성코드, 옵션지정상품코드, 옵션값코드)

class Element(models.Model):
    element_code = models.AutoField(primary_key=True)
    designated_code = models.ForeignKey(Designated, on_delete=models.CASCADE, db_column='designated_code')
    value_code = models.ForeignKey(Value, on_delete=models.CASCADE, db_column='value_code')

    class Meta:
        ordering = ['element_code']

    def __str__(self):
        return str(self.element_code)

# 참여(참여코드, 아이디(fk), 상품코드(fk), 수량)

class Join(models.Model):
    join_code = models.AutoField(primary_key=True)
    username = models.ForeignKey(Member, on_delete=models.CASCADE, db_column='username')
    product_code = models.ForeignKey(Product, on_delete=models.CASCADE, db_column='product_code')
    part_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.join_code)

    class Meta:
        ordering = ['join_code']

# 참여상세(참여상세코드, 수량, 가격, 참여코드, 옵션지정상품코드)

class JoinDetail(models.Model):
    joindetail_code = models.AutoField(primary_key=True)
    join_code = models.ForeignKey(Join, on_delete=models.CASCADE, db_column='join_code')
    designated_code = models.ForeignKey(Designated, on_delete=models.CASCADE, null=True, blank=True, db_column='designated_code')
    quantity = models.IntegerField()
    price = models.IntegerField()

    def __str__(self):
        return str(self.joindetail_code)

    class Meta:
        ordering = ['joindetail_code']
disbfnqx

disbfnqx1#

您没有指定 member 对象进行更新。 member = Member.objects.all() 将得到所有问题的查询集 member 数据库中的对象。
试着做:

def mypage_modify(request):
   current_category = None
   categories = Category.objects.all()
   member = Member.objects.all()

   if not request.user.is_authenticated:
       return HttpResponseRedirect(reverse('zeronine:login'))

   if request.method == "POST":
       member = Member.objects.get(name=request.POST['name']) #specifying the member
       member.name = request.POST['name']
       member.password = request.POST['password']
       member.username = request.user
       member.save()
       return redirect('zeronine:post')

   else:
       memberForm = MemberForm
       return render(request, 'mypage/mypage_modify.html', {'memberForm':memberForm, 'member':member, 'current_category': current_category, 'categories': categories})

或者,如果您正在创建新成员对象,则 member = Member.objects.get(name=request.POST['name']) 做: member = Member() 编辑:
不确定为什么要在实践中这样做,但要更改数据库中的每个成员对象,请执行以下操作:

def mypage_modify(request):
   current_category = None
   categories = Category.objects.all()
   member = Member.objects.all()

   if not request.user.is_authenticated:
       return HttpResponseRedirect(reverse('zeronine:login'))
   if request.method == "POST":
       for m in member:
          m.name = request.POST['name']
          m.password = request.POST['password']
          m.username = request.user
          m.save()
       return redirect('zeronine:post')
cbwuti44

cbwuti442#

你用什么访问 member = Member.objects.all() 这是一份 Member 物体。要获得所需的成员,请使用

member = Member.objects.get()

member = Member.objects.filter()

在后者中,您还需要检查

if member:
    member = member.first()

检查https://docs.djangoproject.com/en/3.2/ref/models/querysets/#filterhttps://docs.djangoproject.com/en/3.2/ref/models/querysets/#get

相关问题