如何处理HTML中的过滤器对象

时间:2018-07-17 08:53:43

标签: django html5

我在上面尝试过,以查看如何在首页中显示对象。

很少有人推荐我在下面使用此代码

infos = Info.objects.select_related('user','group', 'user_number').filter(id = newc.id)

我的模型代码是这样的。

class Group(models.Model):
        name = models.CharField(max_length=20)
        user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

class Type(models.Model) :
        name = models.CharField(max_length=20)
        user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

class Info(models.Model):
        name = models.CharField(max_length=20)
        email = models.EmailField(null=True, blank=True, unique=True)
        memo = models.CharField(max_length=200, null=True)
        birthday = models.CharField(max_length=12,null=True, blank=True)
        group = models.ForeignKey(Group, on_delete=models.CASCADE)
        user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
        regdt = models.DateTimeField(auto_now_add=True)
        updatedt = models.DateTimeField(auto_now_add=True)

class Number(models.Model):
        number = models.CharField(max_length=11)
        info = models.ForeignKey(Info, on_delete=models.CASCADE)
        user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
        type = models.ForeignKey(Type, on_delete=models.CASCADE)
        regdt = models.DateTimeField(auto_now_add=True)
        updatedt = models.DateTimeField(auto_now_add=True)

和我的views.py。

def addcontract(request) :
        if request.user.is_authenticated :
                pk = request.user.id
                name = request.POST['name']
                email = request.POST['email']
                memo = request.POST['memo']
                birthday = request.POST['birthday']
                group_id = request.POST['group_id']
                type_id = request.POST['type_id']
                number = request.POST['number']
                user_id = pk
                newc = Info(
                        name = name, email = email, memo = memo, birthday = birthday,
                        group_id = group_id, user_id = pk)
                newc.save()
                number = Number(number = number, type_id= type_id, user_id = pk, info = newc)
                number.save()
                infos = Info.objects.select_related('user','group', 'user_number').filter(id = newc.id)
                grouplist = Group.objects.filter(user_id = request.user.id)
                typelist = Type.objects.filter(user_id = request.user.id)
                return render(request, 'detail.html',
                        {'infos':infos, 'grouplist':grouplist, 'typelist':typelist})

我之前的问题是

How to connect all related DB with FK in Django?

从对象根本无法显示主页,这就是为什么我尝试签入主页然后出现问题的原因。

user_id仅用于帐户。我需要在查询中将Info_id用作fk 请帮忙。

0 个答案:

没有答案