I am building a project in Django. It's like a blog page where a user can post journals and photos. I am trying to make a page where everything the person posted is displayed in chronological order. My simplified models.py is shown below.
class Writing(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=100)
date_created = models.BigIntegerField(default=0)
class Photo(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=100)
date_created = models.BigIntegerField(default=0)
In this case, I would have to touch two database tables, and then order them by date_created. I heard that I can use itertools chain to loop through these two tables, but I am not sure exactly how. What should I write in views.py?
It's simple in the views.py you can for example:
def my_view(request):
writing = Writing.objects.filter(user=request.user).order_by('-date_created')
photos = Photo.objects.filter(user=request.user).order_by('-date_created')
context = {'writing': writing, 'photos': photos}
return render(request, 'your_template.html', context)
One more hint you shouldn't use BigIntegerField for date, instead of it use DateTime
date_created = models.DateTimeField(auto_now_add=True, blank=True)
Related
class Account(models.Model):
accountId = models.CharField(max_length=20, primary_key=True)
username = models.CharField(max_length=30)
def __str__(self):
return self.username
class Project(models.Model):
projectId = models.CharField(max_length=20, primary_key=True, default=idgen)
projectName = models.CharField(max_length=20)
projectOwner = models.ForeignKey(User, on_delete=models.CASCADE, related_name="owner")
projectSize = models.IntegerField(default=25)
projectGuests = models.ManyToManyField(User, related_name="guests")
projectAccounts = models.ManyToManyField(Account, "accounts")
def __str__(self):
return self.projectId
In this code, users can have multiple projects and projects can have multiple accounts. Is there any way to pass just the 'user' to the template (or without passing anything since we can get the user using the request) to get all the accounts in all the projects that the user owns without using a for loop? cause I have to display a count of accounts in total that the user owns.
for example, if the user had 2 projects and each project had 2 accounts... is there any way to get the final value 4 in the template just using the user object?
Try this Query
accounts = Project.objects.filter(projectOwner=request.user).projectAccounts_set.all()
It is best to perform such arithmetic in Django views rather than templates 21404051.
First select all Projects related to request.user by
projects = Project.objects.filter(projectOwner=request.user)
.annotate(pc = Count('projectAccounts'))
.aggregate(total = Sum('pc'))
Now in template
{{ projects.total }}
I decided to do it in views.py after all since it looks impossible to do in templates.
accs = []
for project in request.user.owner.all():
for account in project.projectAccounts.all():
accs.append(account)
accs = list( dict.fromkeys(accs))
this was a simple solution. pretty sure there are way easier and more efficient ways to do this.
I am working on a project where I want to create a slug for each post based on its title. Is it possible to generate a slug in such a way that it will be unique to the post, but will not change even if the title of the post is changed? I am using the model provided in the file 'model.py'. Can you provide guidance on how to accomplish this?
class Post(models.Model):
username = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
description = models.CharField(('Description'),max_length=250)
title = models.CharField(('Content Title'), max_length=250)
create_date = models.DateTimeField(default = timezone.now)
image_data = models.ImageField(upload_to='User_Posts', height_field=None, width_field=None, max_length=None)
slug = (title)
def __str__(self):
return self.title
I recommend checking out the Django documentation for slugify. You will need to override the save method of your model to do this, so your new code will most likely look something like this:
from django.utils.text import slugify
slug=models.SlugField()
def save(self,*args,**kwargs):
self.slug=slugify(self.title)
super(Post,self).save(*args,**kwargs)
I would keep in mind the unique parameter that you can set to either true or false in your slugfield.
These are two models in my Django app :
models.py
class Posts(models.Model):
title = models.CharField(max_length=200, blank=True)
author = models.ForeignKey(user,on_delete=models.CASCADE,default=None, blank=True)
content = models.TextField()
class Unposted(models.Model):
article = models.ForeignKey(Posts, on_delete=models.CASCADE)
upload_at = models.DateTimeField(default=datetime.now, blank=True)
I'm trying to retrieve data from Posts using an API request to Unposted.
Here's what I have until now but I'm unsure how to get data from the Posts model. Right now I just get a JSON response with only the upload_at field.
resources.py
class UnpostedResource(ModelResource):
class Meta:
queryset = Unposted.objects.all()
resource_name = 'unposted'
If I'm not wrong, u can just import your Posts model and then just by for loop make an array with posts models using foreign key from unposted to filter your posts =) Sounds weird and I'm not sure about effectiveness, but looks pretty nice. It will look smth like:
queryset = Posts.objects.filter(article_in=[get(i.article) for i in Unposted.objects.all()])
In the case, Posts is a foreignkey of Unposted, thus you need to define foreignkey field in the resource for the corresponding field in model, this tutorial maybe can help you.
I'm doing a Django project (kind of social network) and want to have a page where I can see all my posts, which I did.
I allways get the error: no such column: uploaded_by
in models.py
from django.db import models
from django.contrib.auth.models import User
class ContentItem(models.Model):
upload_date = models.DateTimeField(auto_now=True)
title = models.CharField(max_length=100, default='no title')
description = models.CharField(max_length=400, default='no description')
image = models.ImageField(upload_to='image_board/posts/', default='null')
uploaded_by = models.ForeignKey(User, default='0')
def __str__(self):
return self.title
in views.py
def view_my_favorites(request):
all_posts = ContentItem.objects.raw('SELECT * FROM image_board_ContentItem WHERE uploaded_by = request.user.username')
template = loader.get_template('favorites.html')
context = {
'all_posts': all_posts,
}
return HttpResponse(template.render(context, request))
I want to get the user name of the user who is loged in, how can i whrite this in the sql query?
Thaks guys :)
Your actual issue is probably caused by neglecting to make and run migrations after adding the uploaded_by field.
But there are a huge number of other things wrong here.
Firstly, you are comparing the uploaded_by column with a non-existent column, request.user.username. You need to use the actual value of that variable.
Secondly, you are comparing a foreign key - uploaded_by - with a string, username. These will never match.
Thirdly, you are using a raw query. There is absolutely no need to do that here.
Your query is trivial to express in the Django query syntax. You should do:
all_posts = ContentItem.filter(uploaded_by=request.user)
or even simpler:
all_posts = request.user.contentitem_set.all()
I have a blog app that consists of 3 models: department, author, post
I am having trouble structuring the models correctly and creating the corresponding forms
models.py
from django.db import models
class Department(models.Model):
name=models.CharField(max_length=20)
posts = models.ForeignKey('Post')
authors = models.ManyToManyField('Author')
def __unicode__(self):
return self.name
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
post = models.ForeignKey('Post')
def __unicode__(self):
return self.last_name
class Post(models.Model):
title=models.CharField(max_length=20)
post = models.TextField()
creation_date = models.DateField(auto_now_add=True)
def __unicode__(self):
return self.title
The idea is that a department can have many posts, but each post belongs to only one department. A department can also be made up of multiple authors and authors can be in multiple departments. Where I'm really having trouble is with the forms.
The relevant urls.py looks like this:
url(r'^(?P<department_id>\d+)/posts/$', views.posts, name='posts'),
url(r'^(?P<department_id>\d+)/add_post/$', views.add_post, name="add_post"),
So I can pull in all the posts by department. The goal of the form is for the department id to be recognized and added automatically to the post.
def add_post(request, department_id):
department = Department.objects.get(pk=department_id)
if request.method == 'POST':
new_post_form = PostForm(data=request.POST)
if new_post_form.is_valid():
new_post = new_post_form.save(commit=False)
new_post.department = department
new_post.save()
return redirect('posts', department_id=department_id)
Now I realize that the Post model does not have a department attribute, which is the error that I get, but I'm guessing that there's a way to make this happen, I just don't know what it is.
Thanks as always for your help. Please let me know if anything is unclear.
The fact that the Post model does not have a department attribute should have given you the clue that your structure is wrong: it clearly needs one. The issue is that you have your ForeignKey the wrong way round: a FK is a one-to-many relationship, and lives on the "many" side, in your case Post, pointing to the "one", ie the Department.
Then your view code will work exactly as it is, and you can retrieve all posts for a department with my_department.post_set.all().