sql - Django - Modify query based on user logged in -
i have database has 2 tables: 'installs' , 'installers'. 'install' table has field installer. installer able log in , see list of installs.
for now, working sqlite use mysql production.
how do this?
thanks!
models.py:
from django.db import models 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 # should be??? def get_absolute_url(self): return reverse('installers:installer_edit', kwargs={'pk': self.pk})
and views.py (installers not completed):
from django.shortcuts import render,redirect, get_object_or_404 django.forms import modelform, forms #from django.contrib.admin.widgets import admindatewidget 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})
edit: added models.py , views.py
is correct assume installer
model sort of profile-like model user
model?
if so, , once have relation between install
, installer
, things easier, if example defined relation like:
class install(models.model): installer = models.foreignkey('installer', related_name='installs')
as in view:
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)
Comments
Post a Comment