Two slugs in url - django

I want to have a url pattern that takes 2 slugs. I'm trying to make it look like http://127.0.0.1:8000/category/model but I'm having difficulties understanding how to do this.
Below is what I have so far:
models.py
def model_detail_view(request, category_slug, model_slug):
model = Model.objects.get(
category__slug=category_slug, model_slug=model_slug)
context = {
"model": model,
}
return render(request=request, template_name='main/model_detail.html', context=context)
urls.py
path("<str:category_slug>/<str:model_slug>/", views.model_detail_view, name="model_detail_view"),
models.py
class Category(models.Model):
title = models.CharField(max_length=50)
featured_image = models.ImageField(upload_to="categories")
category_slug = AutoSlugField(null=True, default=None,
unique=True, populate_from='title')
class Meta:
verbose_name_plural = "Categories"
def __str__(self):
return self.title
class Model(models.Model):
title = models.CharField(max_length=80)
category = models.ManyToManyField(Category)
featured_image = models.ImageField(upload_to=image_dir)
model_slug = AutoSlugField(null=True, default=None,
unique=True, populate_from='title')
class Meta:
verbose_name_plural = "Models"
def __str__(self):
return self.title

Try this,
model = model = Model.objects.get(category__category_slug=category_slug, model_slug=model_slug)
Reference:
Django:Lookups that span relationships---(Django Doc)

Related

Filter in get_context_data and get_query_set not working

I have a listview where I'm trying to filter out products by category. Some products have a subcategory. When a product has a subcategory I want the listview to display them by subcategory.
Problem is: The listview works perfect for items with a subcategory, but does not work for items who do not have a subcategory. Where am I taking a wrong turn here?
Models:
class Category(models.Model):
category_name = models.CharField(max_length=200)
sub_category = models.CharField(max_length=200,blank=True,null=True)
category_picture = ResizedImageField(upload_to='category/', null=True, blank=True)
category_info = models.TextField(blank=True, null=True)
category_video = models.CharField(max_length=250,blank=True, null=True)
def __str__(self):
if self.sub_category is None:
return self.category_name
else:
return f" {self.sub_category}"
class Meta:
ordering = ['category_name']
class Bottle(models.Model):
category_name = models.ForeignKey('Category', on_delete=models.SET_NULL,null=True,blank=True)
brand = models.ForeignKey('Brand', on_delete=models.CASCADE)
bottle_name = models.CharField(max_length=255)
bottle_info = models.TextField()
bottle_tasting_notes = models.TextField()
bottle_barcode = models.IntegerField()
bottle_image = ResizedImageField(upload_to='bottles/',null=True, blank=True)
bottle_shop_link = models.CharField(max_length=250, null=True, blank=True)
def __str__(self):
return f"{self.brand}, {self.bottle_name}"
class Meta:
ordering = ['bottle_name']
View:
class BottlesByCategoryView(ListView):
model = Bottle
context_object_name = 'bottles'
#Filter bij subcategory in the category model. If no subcategory exists, load by category_name
def get_queryset(self):
if Bottle.objects.filter(category_name__sub_category=self.kwargs['category']) is None:
return Bottle.objects.filter(category_name__category_name=self.kwargs['category'])
else:
return Bottle.objects.filter(category_name__sub_category=self.kwargs['category'])
def get_context_data(self, **kwargs):
context = super(BottlesByCategoryView, self).get_context_data(**kwargs)
if Bottle.objects.filter(category_name__sub_category=self.kwargs['category']) is None:
context['category_info'] = Category.objects.filter(category_name=self.kwargs['category'])
else:
context['category_info'] = Category.objects.filter(sub_category=self.kwargs['category'])
return context
URLS:
path('BottlesByCategory/<str:category>/',BottlesByCategoryView.as_view(template_name='academy/bottlesByCat_list.html'),name='bottlesByCat_list'),
Can i not use if statements in the get_context_data and get_query_set?

Django ModelChoiceField: filtering object based on pk in url

I've read many questions about this topic, but none of the methods work for me.
There are 3 related models:
class Trips(models.Model):
lake = models.CharField("Lake", max_length=150)
city = models.CharField("City", max_length=100, blank=True)
s_date = models.DateTimeField("Starting Date", auto_now=False, auto_now_add=False)
e_date = models.DateTimeField("Ending Date", auto_now=False, auto_now_add=False)
trip_id = models.AutoField(primary_key=True)
class Meta:
verbose_name = "Trip"
verbose_name_plural = "Trips"
def __str__(self):
return f"{self.lake}-{self.trip_id}-{self.s_date}"
class Fisherman(models.Model):
name = models.CharField("Fisherman", max_length=50)
trip = models.ForeignKey(Trips, on_delete=models.CASCADE)
fisherman_id = models.AutoField(primary_key=True)
class Meta:
verbose_name = "Fisherman"
verbose_name_plural = "Fishermen"
def __str__(self):
return f"{self.name}-{self.fisherman_id}"
class Catch(models.Model):
fish_type = models.CharField("Fish Type", max_length=50)
catch_id = models.AutoField(primary_key=True)
weight = models.DecimalField("Weight", max_digits=5, decimal_places=2)
length = models.DecimalField("Length", max_digits=5, decimal_places=2, blank=True, null=True)
datetime = models.DateTimeField("Catch Time", auto_now=False, auto_now_add=False)
fisherman = models.ForeignKey(Fisherman, on_delete=models.CASCADE)
trip = models.ForeignKey(Trips, on_delete=models.CASCADE)
class Meta:
verbose_name = "Catch"
verbose_name_plural = "Catches"
def __str__(self):
return f"{self.fish_type}-{self.catch_id}"
I have a ModelForm to create a new catch. Here I use a ModelChoiceField to list Fishermen, but I don't know how to filter them. I only want display those who belong to the trip.
class CatchForm(forms.ModelForm):
fisherman = forms.ModelChoiceField(queryset= Fisherman.objects.all())
class Meta:
model = Catch
fields = ["fish_type", "weight", "length", "datetime", "fisherman"]
widgets = {
"datetime": forms.DateTimeInput(format='%Y-%m-%d %H:%M', attrs={'class':'datetimefield form-control'}),
}
views.py
I' ve read that get_form_kwargs should be used in views to override fields in the form, but it didn't work for me.
class NewCatchView(CreateView):
model = Catch
form_class = CatchForm
template_name = "new_trip/new_catch.html"
# Probably, this is wrong
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['fisherman'] = Fisherman.objects.filter(trip=self.kwargs.get('pk'))
return kwargs
def form_valid(self, form):
form.instance.trip = Trips.objects.get(pk=self.kwargs['pk'])
return super().form_valid(form)
def get_success_url(self):
return reverse('new_trip:trip_details', args=(self.kwargs['pk'],))
urls.py
path("trip_details/<int:pk>/new_catch/", views.NewCatchView.as_view(), name="new_catch"),
Thank you in advance for your help!
You're almost there. You've created the kwarg, so now you just need to use it in the form to overwrite the original queryset:
class CatchForm(forms.ModelForm):
...
def __init__(self, *args, **kwargs):
fisherman = kwargs.pop('fisherman')
super().__init__(*args, **kwargs)
self.fields['fisherman'].queryset = fisherman

(1048, "Column 'brand_id' cannot be null") in django rest framework

I am trying to create an api where a user can add their products. I am sending raw json data from postman but it is giving this error.
IntegrityError at /api/addproducts
(1048, "Column 'brand_id' cannot be null")
I am sending brand id in the data. I am not sure what is happening.
Here I am sending merchant_id as well as categories ids but why brand field is creating an error I am not sure.
My models:
class Category(models.Model):
name = models.CharField(max_length=100, unique=True)
image = models.ImageField(null=True, blank=True)
class Meta:
verbose_name_plural = "Categories"
def __str__(self):
return self.name
class Brand(models.Model):
brand_category = models.ForeignKey(Category,on_delete=models.CASCADE,blank=True,null=True)
name = models.CharField(max_length=100, unique=True)
image = models.ImageField(null=True, blank=True)
class Meta:
verbose_name_plural = "Brands"
def __str__(self):
return self.name
class Collection(models.Model):
name = models.CharField(max_length=100, unique=True)
image = models.ImageField(null=True, blank=True)
class Meta:
verbose_name_plural = "Collections"
def __str__(self):
return self.name
class Variants(models.Model):
SIZE = (
('not applicable', 'not applicable',),
('S', 'Small',),
('M', 'Medium',),
('L', 'Large',),
('XL', 'Extra Large',),
)
AVAILABILITY = (
('available', 'Available',),
('not_available', 'Not Available',),
)
product_id = models.CharField(max_length=70,default='OAXWRTZ_12C',blank=True)
price = models.DecimalField(decimal_places=2, max_digits=20,default=500)
size = models.CharField(max_length=50, choices=SIZE, default='not applicable',blank=True,null=True)
color = models.CharField(max_length=70, default="not applicable",blank=True,null=True)
variant_image = models.ImageField(upload_to="products/images", blank=True)
thumbnail = ImageSpecField(source='variant_image',
processors=[ResizeToFill(100, 50)],
format='JPEG',
options={'quality': 60})
quantity = models.IntegerField(default=10,blank=True,null=True) # available quantity of given product
variant_availability = models.CharField(max_length=70, choices=AVAILABILITY, default='available')
class Meta:
verbose_name_plural = "Variants"
def __str__(self):
return self.product_id
class Product(models.Model):
merchant = models.ForeignKey(Seller,on_delete=models.CASCADE,blank=True,null=True)
category = models.ManyToManyField(Category, blank=False)
brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
collection = models.ForeignKey(Collection, on_delete=models.CASCADE)
featured = models.BooleanField(default=False) # is product featured?
best_seller = models.BooleanField(default=False)
top_rated = models.BooleanField(default=False)
My serializers:
class BrandSerializer(serializers.ModelSerializer):
# id = serializers.IntegerField()
class Meta:
model = Brand
fields = '__all__'
class AddProductSerializer(serializers.ModelSerializer):
merchant = serializers.PrimaryKeyRelatedField(read_only=True)
class Meta:
model = Product
fields = ['id','merchant','category','brand', 'collection','featured', 'top_rated',
'name','description', 'picture','main_product_image','best_seller',
'rating','availability','warranty','services','variants']
# depth = 1
def create(self, validated_data):
product = Product.objects.create()
return product
My views:
class ProductAddAPIView(CreateAPIView):
permission_classes = [AllowAny]
queryset = Product.objects.all()
serializer_class = AddProductSerializer
i think the problem is in the create function in serializer
class AddProductSerializer(serializers.ModelSerializer):
merchant = serializers.PrimaryKeyRelatedField(read_only=True)
class Meta:
model = Product
fields = ['id','merchant','category','brand', 'collection','featured', 'top_rated',
'name','description', 'picture','main_product_image','best_seller',
'rating','availability','warranty','services','variants']
# depth = 1
def create(self, validated_data):
product = Product.objects.create()
return product
as you didn't pass the validated data so it create an empty object use this instead
def create(self, validated_data):
return Product.objects.create(**validated_data)

How to check the name of a model in a Django Template

I'm trying to get the name of a model in my template so i can give it a different design in the template
#views.py
class PostListView(ListView):
model = Post
template_name = 'blog/home.html'
context_object_name = 'posts'
paginate_by = 15
def get_queryset(self):
posts = []
shared_post = []
if self.request.user.is_authenticated:
user_id = self.request.user.id
view_user_post = Post.objects.filter(user=self.request.user)
user_profile = User.objects.get(id=user_id).profile
# print(user_profile)
for profile in user_profile.follower.all():
for post in Post.objects.filter(user=profile.user):
posts.append(post)
for profile in user_profile.follower.all():
for share in Share.objects.filter(user=profile.user):
shared_post.append(share)
chain_qs = chain(posts, view_user_post, shared_post)
print(chain_qs)
return sorted(chain_qs, key=lambda x: x.date_posted, reverse=True)
else:
posts = Post.objects.all().order_by('?')
return posts
#models.py
class Share(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
content = models.TextField(max_length=140, null=True, blank=True)
date_posted = models.DateTimeField(default=timezone.now)
def __str__(self):
return '{}- {}'.format(self.post.title, str(self.user.username))
class Post(models.Model):
title = models.CharField(max_length=140)
content = models.TextField(validators=[validate_is_profane])
likes = models.ManyToManyField(User, related_name='likes', blank=True)
date_posted = models.DateTimeField(default=timezone.now)
user = models.ForeignKey(User, on_delete=models.CASCADE)
image = models.ImageField(upload_to='post_pics', blank=True)
image_2 = models.ImageField(upload_to='post_pics', blank=True)
image_3 = models.ImageField(upload_to='post_pics', blank=True)
restrict_comment = models.BooleanField(default=False)
saved = models.ManyToManyField(User, related_name='saved_post', blank=True)
I need a way to check the name of the model in the template possibly an if/ else statement to check properly. thanks
What about create a function inside your model that will return the name of the model?
Inside your models.py for each model:
def get_my_model_name(self):
return self._meta.model_name
Inside your template then yo can do something like:
{%if post.get_my_model_name == 'post'%}
Do something ...
Instead of checking the model name I suggest you implement a boolean property in each model that returns True in one case and False in the other one. For example:
class Post(models.Model):
# whatever fields and methods
#property
def is_shared(self):
return False
class Share(models.Model):
# whatever fields and methods
#property
def is_shared(self):
return True
Then in your template just check {% if post.is_shared %}

Django foreign key and one-to-many relationship

I'm trying to build a basic ecommerce website, as a mean to learning django. I'm trying to build my models, so that there is a Category, a Product and Product Image class to store my data in. This is my models.py:
class Category(models.Model):
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True, unique=True)
class Meta:
ordering = ('name', )
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('products:product_list_by_category', args=[self.slug])
class Product(models.Model):
category = models.ForeignKey(Category, related_name='products')
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True)
description = models.TextField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
stock = models.PositiveIntegerField()
available = models.BooleanField(default=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ('name', )
index_together = (('id', 'slug'), )
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('products:product_detail', args=[self.id, self.slug])
class ProductImage(models.Model):
property = models.ForeignKey('Product', related_name='images', on_delete=models.CASCADE)
image = models.ImageField(upload_to='products')
And this is my views.py:
def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
product_img = Product.images.all()
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
return render(request, 'products/list.html', {'category': category, 'categories': categories, 'products': products, 'product_img': product_img})
I've seen similar post here on stack overflow, and have tried to do the same thing, but I still get the following error: AttributeError: 'ReverseManyToOneDescriptor' object has no attribute 'all'
Where am I going wrong here?