Django数据库对象无法显示

时间:2016-09-14 10:16:03

标签: python django database shell object

每当我尝试通过shell中的<QuerySet [<Member: - >]>显示模型类中的所有对象时,我会不断获得Member.objects.all()

这是我的代码,请注意当我调用所有对象时,Book类正确显示它们,但是Member类没有:

class Book(models.Model):
    Titel = models.CharField(max_length=100)
    Author = models.CharField(max_length=100)
    Genre = models.CharField(max_length=100)
    ISBN13 = models.CharField(max_length=13)

    def __str__(self):
        return self.Author + ' - ' + self.Titel

class Member(models.Model):
    Name = models.CharField(max_length=15)
    Surname = models.CharField(max_length=15)
    Mailadres = models.CharField(max_length=15)
    Birth = models.DateField(auto_now_add=True, auto_now=False)
    Leerlingnummer = models.CharField(max_length=15)

    def __str__(self):
        return self.Name + ' - ' + self.Surname

按要求添加成员的方式:

from dashboard.models import Member
a = Member()
Name = 'xxx'
Surname = 'xxx'
Mailadres = 'xxxxxxx'
Birth = 'xx-xx-xxxx'
Leerlingnummer = 'xxxxxxxxx'
a.save()

1 个答案:

答案 0 :(得分:0)

您只是创建了一些变量。您必须分配您创建的对象的属性并实际保存它。

from dashboard.models import Member
a = Member()
a.Name = 'xxx'
a.Surname = 'xxx'
a.Mailadres = 'xxxxxxx'
a.Birth = 'xx-xx-xxxx'
a.Leerlingnummer = 'xxxxxxxxx'
a.save()

或者,您可以使用Member.objects.create

from dashboard.models import Member
a = Member.objects.create(
        Name='xxx',
        Surname='xxx',
        Mailadres='xxxxxxx',
        Birth='xx-xx-xxxx',
        Leerlingnummer='xxxxxxxxx')