Where exactly are you trying to insert a user ID? On the Product class? During what operation?
-James
On Mon, Aug 31, 2015 at 4:58 AM, Remaze Vs <remaze.vs@gmail.com> wrote:
I am trying to insert session login user id in database... here is my code--view.pyfrom django.shortcuts import render,redirect, Http404from django.forms import ModelForm# Create your views here.from marketing.forms import EmailFormfrom marketing.models import MarketingMessage, Sliderfrom .models import Product, ProductImage,Book,Category,Productfrom products.models import Productclass DealsForm(ModelForm):class Meta:model = Productfields = ['title','description','category','price','sale_price','slug','active','update_defaults','user']exclude = ('user',)def deals(request, template_name='products/deals.html'):form = DealsForm(request.POST or None)if form.is_valid():form.save()return redirect('home')return render(request, template_name, {'form':form})Model.py filefrom django.conf import settingsfrom django.core.urlresolvers import reversefrom django.db import modelsfrom django.db.models.signals import post_savefrom django.contrib.auth.models import Userclass Book(models.Model):name = models.CharField(max_length=200)pages = models.IntegerField()email = models.CharField(max_length=200)def __unicode__(self):return self.nameclass Category(models.Model):title = models.CharField(max_length=120)description = models.TextField(null=True, blank=True)slug = models.SlugField(unique=True)featured = models.BooleanField(default=None)timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)updated = models.DateTimeField(auto_now_add=False, auto_now=True)active = models.BooleanField(default=True)def __unicode__(self):return self.titleclass Product(models.Model):title = models.CharField(max_length=120)description = models.TextField(null=True, blank=True,max_length=200)category = models.ManyToManyField(Category, null=True, blank=True)price = models.DecimalField(decimal_places=2, max_digits=100, default=29.99)sale_price = models.DecimalField(decimal_places=2, max_digits=100,\null=True, blank=True)slug = models.SlugField(unique=True)timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)updated = models.DateTimeField(auto_now_add=False, auto_now=True)active = models.BooleanField(default=True)update_defaults = models.BooleanField(default=False)user = models.ForeignKey(User)def __unicode__(self):return self.titleclass Meta:unique_together = ('title', 'slug')def get_price(self):return self.pricedef get_absolute_url(self):return reverse("single_product", kwargs={"slug": self.slug})def my_view(request):product = DealsForm.save(commit=False)product.user = request.usernameproduct.save()class ProductImage(models.Model):product = models.ForeignKey(Product)image = models.ImageField(upload_to='products/images/')featured = models.BooleanField(default=False)thumbnail = models.BooleanField(default=False)active = models.BooleanField(default=True)updated = models.DateTimeField(auto_now_add=False, auto_now=True)def __unicode__(self):return self.product.titleclass VariationManager(models.Manager):def all(self):return super(VariationManager, self).filter(active=True)def sizes(self):return self.all().filter(category='size')def colors(self):return self.all().filter(category='color')VAR_CATEGORIES = (('size', 'size'),('color', 'color'),('package', 'package'),)class Variation(models.Model):product = models.ForeignKey(Product)category = models.CharField(max_length=120, choices=VAR_CATEGORIES, default='size')title = models.CharField(max_length=120)image = models.ForeignKey(ProductImage, null=True, blank=True)price = models.DecimalField(max_digits=100, decimal_places=2, null=True, blank=True)updated = models.DateTimeField(auto_now_add=False, auto_now=True)active = models.BooleanField(default=True)objects = VariationManager()def __unicode__(self):return self.titledef product_defaults(sender, instance, created, *args, **kwargs):if instance.update_defaults:categories = instance.category.all()print categoriesfor cat in categories:print cat.idif cat.id == 1: #for t-shirtssmall_size = Variation.objects.get_or_create(product=instance,category='size',title='Small')medium_size = Variation.objects.get_or_create(product=instance,category='size',title='Medium')large_size = Variation.objects.get_or_create(product=instance,category='size',title='Large')instance.update_defaults = Falseinstance.save()#print args, kwargspost_save.connect(product_defaults, sender=Product)can someone help me..?
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ddb826d8-d0b7-400c-b300-7e570ab5402f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciX%2BOeSRG2w6tE6CmevG4RnE8RE%2BsfVZv4B6a%2BhaUY6vGA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment