I have searched and read through the internet trying to figure out this problem. Thank you for any advice on this issue.
I have been having problems adding a list of objects to another object in Django. I have an object 'category' and a list of objects 'subcategory', but when I try to put them together as a package 'ad', there is a TypeError: 'subcategory' is an invalid keyword argument for this function.
Here is the view:
def create_in_category(request, slug):
category = get_object_or_404(Category, slug=slug)
subcategory = SubCategory.objects.all()
ad = Ad.objects.create(category=category, subcategory=subcategory, user=request.user,
expires_on=datetime.datetime.now(), active=False)
ad.save()
What am I missing to be able to get all of these elements together? Thanks very much for sharing your knowledge.
Edit: added the models.
class Category(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField()
def __unicode__(self):
return self.name + u' Category'
class SubCategory(models.Model):
name = models.CharField(max_length=50, unique=True)
category = models.ManyToManyField(Category)
def __unicode__(self):
return self.name + u' SubCategory'