adding badges to user for doing tasks in django - python

Hi Djangonauts,
How is everyone today, I am new to Django so please forgive any silly mistakes in logic or code. I have a user, a User_Profile and a posts model. I want to give the user badges for the number of posts the user posts. Example if the user posts 3 posts he/she gets a beginner badge and so on...
Below is my user Profile models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_image = models.ImageField(default='', blank=True, null=True)
badge_image = models.ImageField(default='', blank=True, null=True)#maybe default can be "static/images/beginner_image.jpg" but it does not work
posts models.py
User = get_user_model()
class Post(models.Model):
user = models.ForeignKey(User, related_name='posts')
title = models.CharField(max_length=250, unique=True)
slug = models.SlugField(allow_unicode=True, unique=True)
message = models.TextField()
Now I want to achieve something like this:
user = User.objects.get(username=request.user.username)
if user.posts.count >= 3 :
badge_image = '<img src="some image">'# I don't know how to make a image variable in views and models. I know how to use it in Django templates with {{image.url}}
elif user.posts.count >= 20 :
badge_image = '<img src="some image">'
else:
badge_image ='<img src="beginner_image">'
Where do I write this code in the models, in the views? I am sorry if my questions are too basic. None of the projects I learned had a similar scenario

You can do it without ImageField. Use model's #property for this. You need to copy badge images(e.g. badge_img1.jpg, badge_img2.jpg, etc.) to director with static files(check docs how to manage static with Django). Rewrite Profile like this:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_image = models.ImageField(default='', blank=True, null=True)
#property
def badge_image(self):
if self.user.posts.count() >= 20:
badge_image = 'badge_img1.jpg'# I don't know how to make a image variable in views and models. I know how to use it in Django templates with {{image.url}}
elif self.user.posts.count() >= 3:
badge_image = 'badge_img2.jpg'
else:
badge_image ='badge_img3.jpg'
return badge_image
you can display current user's image like this:
{% load static %}
<img src={% static user.profile.badge_image %}>

Related

display objects from inline model in DetailView

I have 2 models Product and Resource. Resource has to be a TabularInline model in my admin panel. I am struggling with filtering resource titles that are related only to this product. Since it is a ForeignKey I should use select_related but I am not sure how to use it in my case. For now, the loop in my HTML file gives me all sales files (from all products).
models.py
class Product(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField('title', max_length=400, default='')
slug = models.SlugField(unique=True, blank=True, max_length=600)
class Resource(models.Model):
id = models.AutoField(primary_key=True)
type = models.CharField(max_length=32, choices=RESOURCE_TYPE, default='sales')
title = models.CharField(max_length=400, blank=False, null=False, default='')
related_files = models.FileField(upload_to='recources/', null=True, blank=True)
publish = models.DateTimeField('Published on', default=timezone.now)
resources = models.ForeignKey(Product, default='', on_delete=models.PROTECT, null=True, related_name='resources')
admin.py
class Resourceinline(admin.TabularInline):
model = Resource
class ProductAdmin(ImportExportModelAdmin):
inlines = [
Resourceinline,
]
resource_class = ProductResource
admin.site.register(Product, ProductAdmin)
views.py
class ProductDetailView(DetailView):
template_name = 'ProductDetailView.html'
model = Product
def get_context_data(self, **kwargs):
context = super(ProductDetailView, self).get_context_data(**kwargs)
resources_sales = Resource.objects.select_related('resources').filter(resources_id =1, type='sales') # not sure what to put here
context['resources_sales'] = resources_sales
return context
ProductDetailView.html
{% for resource in resources_sales.all %}
<p>{{resource.title}}</p>
{% endfor %}
Question
Where am I making the mistake and how can I display resource objects that are related to type=sales and are related only to this product in DetailView.
Edit
I realized that there is a column named resources_id that is connecting both models. Now I am struggling to filter it by id of current DetailView. I put resources_id=1 in my views.py but it must relate to DetailView that user is currently looking at. I tied to put resources_id=self.kwargs['id'] but it gives me KeyError at /product/test-product/ 'id' How can I do that?
since you are using generic DetailView you can refer to the current object with self.get_object(). actually that return the single object that view display. however you can use instateself.object too.
so you can filter the Product related Resources using Resource.objects.filter(resources=self.get_object(), type='sales')
you can read more Single object mixins

How can I edit many to many relations in Django (admin sites)

I need to add tags to my blog posts. Each tag should be available on many posts and also posts can have multiple tags.
How can I change the admin sites so that I can add multiple (existing) tags to a post?
The standard view only lets me add by creating new ones.
model.py
# blog post tags
class Tag(models.Model):
name = models.CharField(max_length=20)
slug = models.SlugField(max_length=40, unique=True)
date_created = models.DateTimeField(default=timezone.now)
class Meta:
ordering = ['name']
def __str__(self):
return self.name
# blog posts
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
last_modified = models.DateTimeField(auto_now=True)
author = models.ForeignKey(User, on_delete=models.CASCADE) # 1 author per post
tags = models.ManyToManyField(Tag, related_name='tags') # n tags per m posts
class Meta:
ordering = ['title']
def __str__(self):
return self.title
Current Admin Site
I know I need to edit my admin.py file in my blog application but everything i tried so far did not work. Is there a recipe for these admin views?
I want to achieve something like this (1st answer - filtered view).

Django ORM relations one-to-many

I have a model:
class Product(models.Model):
title = models.CharField(max_length=200)
url = models.URLField()
pub_date = models.DateTimeField()
votes_total = models.IntegerField(default=1)
image = models.ImageField(upload_to='images/')
icon = models.ImageField(upload_to='images/')
body = models.TextField()
hunter = models.ForeignKey(User, on_delete=models.CASCADE)
Now I'd like to add a functionality of upvoters to know on what products user has already voted. I need this to allow users vote on the one product only once.
Again, to clarify - user can vote on several products but only once on each.
So the relation is one product - many users (upvoters).
I tried to add the next field but cannot make a migration even if default field is provided. Also I tried to clear the database but again cannot make it work.
upvoters = models.ForeignKey(User, on_delete=models.CASCADE, related_name='upvoted')
I suppose it works the next way:
Field to determine upvoted products.
To check if user has been upvoted on product, call: User.upvoted.filter(id=product.id).count() == 1
This means that user has already upvoted on this product.
What's wrong? What should I change to make it work?
You will have to use ManyToMany, but you can use a custom through model to restrict the product/vote combinations.
To Product class, add:
voters = models.ManyToManyField(User, through='ProductVote', related_name='product_voters')
Then add the custom through model:
class ProductVote(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
product = models.ForeignKey(Vote, on_delete=models.CASCADE)
class Meta:
unique_together = ['user', 'product']
If you try to add a vote for the same user/product combination, an IntegrityError will be raised.

Get site-specific user profile fields from user-created object

I am using Django sites framework (Django 2.1) to split an app into multiple sites. All of my models except the User model are site-specific. Here is my Post model:
post.py
class Post(models.Model):
parent = models.ForeignKey(
'self',
on_delete=models.CASCADE,
related_name='children',
related_query_name='child',
blank=True,
null=True,
)
title = models.CharField(
max_length=255,
blank=True,
)
body_raw = models.TextField()
body_html = models.TextField(blank=True)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
site = models.ForeignKey(Site, on_delete=models.CASCADE)
on_site = CurrentSiteManager()
I have no problem separating posts out by site. When I want to get the posts, I call:
posts = Post.on_site.filter(...)
I have a separate model called UserProfile. It is a many-to-one profile where there is a unique profile created for each user-site combination (similar to profile implementation at SE). The profile has a reputation attribute that I want to access when I get any post. This reputation attribute should be different for each site (like how on SE you have different rep on each site you are a member of).
user_profile.py
class UserProfile(models.Model):
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
reputation = models.PositiveIntegerField(default=1)
site = models.ForeignKey(Site, on_delete=models.CASCADE)
on_site = CurrentSiteManager()
How do I access the user's username (on the User model) as well as the user's reputation (on the UserProfile model) when I get Posts from a query?
I'd like to do something like:
Post.on_site.filter(...)
.select_related('user__userprofile')
.filter_related(user__userprofile.site == get_current_site())
How do I filter a Many-To-One related model?
Better to make UserProfile -> User relationship to be OnetoOne,
because Django doesn't know which of many profiles to show
(but you also need to define related_name)
models.OneToOneField(get_user_model(), related_name='userprofile_rev')
Then you will be able to do this
qs = Post.on_site.filer().select_related('user', 'user__userprofile_rev')
for post in qs:
print(post.user.username, post.user.userprofile_rev.reputation)
If you don't want to change your DB structure you can do like this
(but you need to specify which profile to return)
qs = Post.on_site.filer().select_related('user').prefetch_related('user__userprofile_set')
for post in qs:
print(post.user.username, post.user.userprofile_set[0].reputation)

Delete a post in Django

I new to django and I am trying to make a web application. I have this page page image and I want to delete one post when I press the delete button. How can I do that? This is my modal for 'Post' :
class Post(models.Model):
created_date = models.DateTimeField()
title = models.CharField(max_length=100)
profile_image = models.ImageField(upload_to='poze', blank=True, null=True)
text = models.CharField(max_length=1000, default='Nimic', blank=True)
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
I've been looking for delete methods, but I've always found form-only methods and I don't use form. Thank you.
In you html :
Delete
assuming that you are using a for loop :
{% for p in posts %}
In your urls :
path('delete/<post_id>',views.delete_post,name='delete')
In your views :
def delete_post(request,post_id=None):
post_to_delete=Post.objects.get(id=post_id)
post_to_delete.delete()
return HttpResponseRedirect(#name of the view function that returns your posts page)
And that's it
EDIT
This method deletes data from your database directly. So I recommend you add the #login_required decorator to your delete_post view function to protect your post. You can also make it accessible only for admin users or post owners in your html (Example : only users who have staff role can see the delete link)
{% if user.is_staff %}
<a ...>Delete</a>
{% endif %}
You need to pass argument with post id. It would like something like this
p = Post.objects.get(pk=2)
p.delete()
You should create a new field in the database table. Whenever you are deleting the data then you should change the delete field.
class Post(models.Model):
created_date = models.DateTimeField()
title = models.CharField(max_length=100)
profile_image = models.ImageField(upload_to='poze', blank=True, null=True)
text = models.CharField(max_length=1000, default='Nimic', blank=True)
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
delete_flag = models.BooleanField(default=False)
And whenever you are should the data then you should filter the data on the delete flag
Post.objects.filter(delete_flag=False)
Though it is subjective and I don't know you use case, but still as a beginner its better to start with this practice.
Read More
Article 2

Categories

Resources