I would like to return in str method of the Chapter, title of the Chapter and title of the Book (from book model). I need it for better display at the admin pages. How to do that?
Should I create some new method in Book or use Model instance reference https://docs.djangoproject.com/en/1.11/ref/models/instances/#model-instance-methods ?
Or maybe I should use Retrieving specific objects with filters?
https://docs.djangoproject.com/en/1.11/topics/db/queries/#retrieving-objects
I am new to the django, thank you in advance for the solutions.
class Book(models.Model):
author = models.CharField(max_length=200)
title = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
def __str__(self):
return self.title+" ("+self.author+")"
def book_author(self):
return self.author
class Chapter(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
title = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
body = models.TextField()
hierarchy = models.IntegerField()
sort = models.IntegerField(unique=True)
def __str__(self):
print(repr(Book))
print(repr(Book.author))
# print(repr(Book.objects.all().filter(id==1)))
# print(repr(Book.objects.get(id=1)))
print(repr(Book.book_author(self)))
return self.title+"(book: "+")"
def __str__(self):
return '%s (%s)' % (self.title, self.book.title,)
To print your object data in admin panel more nicely you should overide __str__ method for your models.
For example your code should look like:
class Book(models.Model):
author = models.CharField(max_length=200)
title = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
def __str__(self):
return 'Book with title: {} writen by {}'.format(self.title, self.author)
`
Related
i really need help with this:
I have 3 models say User,Item and Comment where User is a foreign key in Item and Item is a foreign key in Comment.
i want to get all comment belonging to a particular user in my view, how do i achieve it. bellow is are my model
class User(models.Model):
name= models.CharField(max_length=200,null=True)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
class Item(models.Model):
user = models.ForeignKey(User, on_delete=CASCADE)
name= models.CharField(max_length=200,null=True)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
class Comment(models.Model):
user = models.ForeignKey(Item, on_delete=CASCADE)
name= models.CharField(max_length=200,null=True)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
there is a problem of design model in your code.
here is how you can do it.
class Comment(models.Model):
item = models.ForeignKey(Item, on_delete=CASCADE) # the related item
author = models.ForeignKey(User,on_delete=CASCADE) #new ( the author of the comment)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
now in your views.py you can do something like this.
Comment.objects.filter(author=request.user)
UPDATED 2
You should consider changing model, like amadou-sow said, that is right way to do such things
UPDATED
def get_user_comments(request):
qs = Comment.objects.filter(user__user=request.user)
You could access yours related objects with "__" (double _) read more in official docs
And you really should rename "user" field to "item" in Comment model if it is foreign key to Item model, like this:
class User(models.Model):
name= models.CharField(max_length=200,null=True)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
class Item(models.Model):
user = models.ForeignKey(User, on_delete=CASCADE)
name= models.CharField(max_length=200,null=True)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
class Comment(models.Model):
item = models.ForeignKey(Item, on_delete=CASCADE)
name = models.CharField(max_length=200,null=True)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
#view.py
def get_user_comments(request):
return Comment.objects.filter(item__user=request.user)
I have three models as follows.
class Quiz(models.Model):
name = models.CharField(max_length=50)
desc = models.CharField(max_length=500)
number_of_questions = models.IntegerField(default=1)
time = models.IntegerField(help_text="Duration of the quiz in seconds", default="1")
def __str__(self):
return self.name
def get_questions(self):
return self.question_set.all()
class Question(models.Model):
ques = models.CharField(max_length=200)
quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)
def __str__(self):
return self.ques
class Answer(models.Model):
content = models.CharField(max_length=200)
correct = models.BooleanField(default=False)
question = models.ForeignKey(Question, on_delete=models.CASCADE)
def __str__(self):
return f"question: {self.question.ques }, answer: {self.content}, correct: {self.correct}"
I have to create api for create question. for this I have to use model Question and Answer same time e.g.
its admin page but I want to create same api which accept above parameters and store it into database.
how to write view.py for this api ?
I'm new to django (and python) and I have a question.
I have this model:
class Material(models.Model):
name = models.CharField(max_length=256, blank=True)
description = models.TextField(null=True, blank=True)
def __str__(self):
return self.name
class Meta:
abstract = True
and
class Experiments(models.Model):
title = models.CharField(max_length=200)
experiment = models.TextField(null=True, blank=True)
def __str__(self):
return self.title
I want to query it so I have only have the Materials used in the Experiments model. I was reading about Q objects but I'm not sure how to use it. I appreciate any help!
What I want to do is simply match the images to the department model through tags. How do I write a view for my department page to do this? I have looked everywhere but can't find the answer. Please help!
Models:
class Tag(models.Model):
tag_title = models.CharField(max_length=200)
slug = models.CharField(max_length=200)
def __str__(self):
return self.tag_title
class Image(models.Model):
image_url = models.CharField(max_length=200)
title = models.CharField(max_length=200)
tags = models.ManyToManyField(Tag, blank=True)
def __str__(self):
return self.title
class Department(models.Model):
page_title = models.CharField(max_length=200)
slug = models.CharField(max_length=200)
content = models.TextField()
tags = models.ManyToManyField(Tag, blank=True)
def __str__(self):
return self.page_title
Try this:
Image.objects.filter(tags__department=d) #where d is your deparment.
Something like this should work:
Given a Department object department_obj:
images = Image.objects.filter(tags__in=department_obj.tags.all())
I am new to Python and Django.
I am trying to build myself very simple blog application.
So I have this 2 models :
class Tag(models.Model):
name = models.CharField(max_length=250)
slug = models.SlugField(unique=True)
def __unicode__(self):
return self.name
class Blogpost(models.Model):
title = models.CharField(max_length=300)
content = tinymce_models.HTMLField()
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
tags = models.ManyToManyField(Tag)
def __unicode__(self):
return self.title
As you can see Blogpost can contain many Tags,
my question is how can I query Blogpost.objects.all() to get Blogposts list by specific Tag?
Thank you.
I think related manager is your answer
t = Tag.objects.get(name="Some tag")
t.blogpost_set.all()