--- models py --- class Product models Model user models ForeignKey Us

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# --- models.py ---
class Product(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=256)
product_id = models.SmallIntegerField(null=True, blank=True)
major_version = models.SmallIntegerField(null=True, blank=True)
minor_version = models.SmallIntegerField(null=True, blank=True)
class Meta:
unique_together = ('name', 'user')
# --- forms.py ---
class ProductForm(forms.ModelForm):
class Meta:
model = Product
exclude = ('user', 'record_status')
# --- views.py ---
@render_to('keeper/products.html')
def products(request):
form = ProductForm()
if request.method == 'POST':
form = ProductForm(request.POST)
if form.is_valid():
instance = form.save(commit=False)
instance.user = request.user
instance.save()
return {'objects': request.user.product_set.all(),
'form': form}