Django - 根据用户登录修改查询

时间:2015-08-07 17:37:52

标签: sql django

我有一个包含两个表的数据库:'installs'和'installers'。 'install'表有一个安装程序字段。我希望安装程序能够登录并查看仅安装的列表。

目前,我正在使用SQLite,但将使用MySQL进行制作。

我该怎么做?

谢谢!

models.py:

from django.db import models
from django.core.urlresolvers import reverse

class Install(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=45)
    address = models.CharField(max_length=60)
    city = models.CharField(max_length=45)
    state = models.CharField(max_length=15)
    zipcode = models.IntegerField()
    email = models.EmailField(max_length=60, blank = True, null = True)
    phone= models.IntegerField(blank = True, null = True)
    website = models.URLField(max_length=90, blank = True, null = True)
    unit_model = models.CharField(max_length=30)
    serial = models.CharField(max_length=30)
    unit_installer = models.CharField(max_length=30)
    unit_dealer = models.CharField(max_length=30)
    unit_distributor = models.CharField(max_length=30)
    install_date = models.DateField()

    def __str__(self): # unicode in python 2
        return self.name 

    def get_absolute_url(self):
        return reverse('installs:install_edit', kwargs={'pk': self.pk})

class Installer(models.Model):
    id = models.AutoField(primary_key=True)
    installer_id = models.CharField(max_length=20)
    name = models.CharField(max_length=40)
    company = models.CharField(max_length=40)
    address = models.CharField(max_length=60)
    city = models.CharField(max_length=40)
    state = models.CharField(max_length=15)
    zipcode = models.IntegerField()
    email = models.EmailField(max_length=60, blank = True, null = True)
    phone= models.IntegerField(blank = True, null = True)
    website = models.URLField(max_length=90, blank = True, null = True)
    trained_by = models.CharField(max_length=20)

    def __str__(self): # unicode in python 2
        return self.name # what should this be???

    def get_absolute_url(self): 
        return reverse('installers:installer_edit', kwargs={'pk': self.pk})

并且views.py(安装程序未完成):

from django.shortcuts import render,redirect, get_object_or_404
from django.forms import ModelForm, forms
#from django.contrib.admin.widgets import AdminDateWidget
from bdb.models import Install, models

class InstallForm(ModelForm):

    class Meta:
        model = Install
        fields = ['name', 'address', 'city', 'state', 'zipcode', 'email', 'phone', 'unit_model', 'unit_discharge',
        'gpd', 'ptgals', 'rgals', 'serial', 'unit_installer', 'unit_dealer', 'unit_distributor', 'install_date']


class InstallerForm(ModelForm):
    class Meta:
        model = Installer
        fields =['name', 'address', 'city', 'state', 'zipcode', 'email', 'phone', 'website', 'installerID', 'trainedBy']

def install_list(request, template_name='installs/installs_list.html'):
    install = Install.objects.all()
    data = {}
    data['object_list'] = install
    return render(request, template_name, data)

def install_create(request, template_name='installs/installs_form.html'):
    form = InstallForm(request.POST or None)
    if form.is_valid():
        form.save()
        return redirect('installs:install_list')
    return render(request, template_name, {'form':form})

def install_update(request, pk, template_name='installs/installs_form.html'):
    cust = get_object_or_404(Install, pk=pk)
    form = InstallForm(request.POST or None, instance=cust)
    if form.is_valid():
        form.save()
        return redirect('installs:installs_list')
    return render(request, template_name, {'form':form})

def install_delete(request, pk, template_name='installs/installs_confirm_delete.html'):
    cust = get_object_or_404(Install, pk=pk)
    if request.method=='POST':
        cust.delete()
        return redirect('installs:installs_list')
    return render(request, template_name, {'object':cust})

编辑:添加了models.py和views.py

1 个答案:

答案 0 :(得分:1)

假设您的Installer模型是User模型的某种类似个人资料的模型,这是正确的吗?

如果是这样,一旦你拥有InstallInstaller之间的关系,事情就会容易得多,例如你定义的关系如下:

class Install(models.Model):
    installer = models.ForeignKey('Installer', related_name='installs')

正如您在视图中所做的那样:

def install_list(request, template_name='installs/installs_list.html'):
    installs = request.user.installer.installs.all()
    data = {}
    data['object_list'] = install
    return render(request, template_name, data)