how to define a copy method in django models - django

i have three models for a blog , shown blow:
class Author(models.Model):
name = models.CharField(max_length = 50)
class BlogPost(models.Model):
title = models.CharField(max_length = 250)
body = models.TextField()
author = models.ForeignKey(Author,on_delete = models.CASCADE)
date_created = models.DateTimeField(auto_now_add = True)
def copy():
pass
class Comment(models.Model):
blog_post = models.ForeignKey(BlogPost, on_delete = models.CASCADE)
text = models.TextField(max_length = 500)
i want to define a copy() method for BlogPost model that copies a BlogPost instance with copieng related comment instances . how can i do this?

You can iterate through the related comments of a given BlogPost instance and make a copy of each comment by nulling its pk attribute, then assign the blog_post foreign key to self and save.
def copy(self, post):
for comment in post.comment_set.all():
comment.pk = None
comment.blog_post = self
comment.save()

Related

how to add category to blog project django

i developed a blog project by watching many open-source courses and create my own django custom admin dashboard where i want to add a category option to my blog project, i have watched some tutorial on as well but couldn't find them helpful
models.py
from django.db import models
from django.forms import ModelForm
from farmingwave.models import BaseHeader,Submenu
class Category(models.Model):
mainmenu=models.ForeignKey(BaseHeader,null=True,on_delete=models.SET_NULL)
submenu=models.ForeignKey(Submenu,on_delete=models.CASCADE)
class AdSideMenu(models.Model):
title_id = models.AutoField(primary_key=True)
title_name = models.TextField()
url = models.TextField()
priority = models.IntegerField()
submenu_status = models.TextField()
class Meta:
db_table = 'admin_side'
class CreateBlog(models.Model):
id = models.AutoField(primary_key=True)
blog_Title = models.TextField(max_length=100)
content = models.TextField(max_length=5000)
category = models.ForeignKey(Category,null=True,on_delete=models.SET_NULL)
class Meta:
db_table = 'create_blog'
they are inhereting data from another app
models.py
`class BaseHeader(models.Model):
main_id = models.AutoField(primary_key=True)
title_name = models.TextField()
url = models.TextField()
priority = models.IntegerField()
submenu_status = models.TextField("false")
class Meta:
db_table = 'base_header'
class Submenu(models.Model):
sub_id = models.AutoField(primary_key=True)
main_id = models.IntegerField()
sub_name = models.TextField()
url = models.TextField()
priority = models.IntegerField()
mainmenu=models.ForeignKey(BaseHeader,on_delete=models.CASCADE)
class meta:
db_table = 'base_subheader'`
and the view function:
def create_blog(request):
if request.method =='POST':
form = CreateBlogForm(request.POST)
if form.is_valid():
form.save()
form = CreateBlogForm()
else:
form = CreateBlogForm()
base = BaseHeader.objects.all()
sub = Submenu.objects.all()
create = CreateBlog.objects.all()
category = Category.objects.all()
context = {
'form' : form,
'createblog' : create,
'category' : category,
'menu' : base,
'sub_menu' : sub,
Why not make the category a select item?
CATEGORY_CHOICES = (
('sports', 'sports'),
('tech', 'tech'),
('politics', 'politics')
)
category = models.CharField(max_length=100, choices=CATEGORY_CHOICES, blank=False)
You'd be able to access it like any other field now, so let's say the user clicked on "Politics articles" you can add a .filter(category="politics") and access it in the templates through {{ article.category }}
I don't know why there are all of these lines in your code, nor do I know the scale of your project, but that's how I would go about doing it.

fields in class Meta got invalid

models.py
class Product(models.Model):
title = models.CharField(max_length=200)
description = models.TextField()
price = models.DecimalField(decimal_places=5,max_digits= 1500)
summary = models.TextField()
featured = models.BooleanField()
def __str__(self):
return self.title
# return f'product title:{self.title}-product price:{self.price}'workok
class Meta:
ordering = ('-price',)
class Opinion(models.Model):
name = models.CharField(max_length=20)
email = models.EmailField(max_length=20)
body = models.TextField()
opinion_date = models.DateTimeField(auto_now_add=True)
active = models.BooleanField(default=False)
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='opinion_set')
def __str__(self):
return f'({self.name}) add opinion about ({self.product})'
forms.py:
from django.forms import ModelForm
from .models import Product #space after from keyword
class OpinionModelForm(ModelForm):
class Meta:
model = Product
fields = ['name','email','body','product']
invalid in code line :
fields = ['name','email','body','product'] #---- NOT WORK !!!
, but if i change above code to :
fields = "__all__" # ----it is WORKing ok without any problem !!
question : what is the error? I am not need all the fields in the Product model (like active boolean field), I need only 'name','email','body','product' fields .
According to the error and the code you provided the main problem is that you made a mistake in chosing model in serializer:
class OpinionModelForm(ModelForm):
class Meta:
model = Product
fields = ['name','email','body','product']
Serializer name is OpinionModelForm and listed fields belong to Opinion so I guess you actually wanted to serialize Opinion and no Product as you defined at this line:
model = Product
Simply change it to:
model = Opinion

Django form field display name

I m trying to make a form on the basis of a model. Trouble is that when i create a category via django shell, let's say "Wedding" category, having id=1 and name = "weddings", when i display it in a dropdown(in html form) it shows as Categories object (1) and i would like it to be shown by the name, which is weddings.
As in the documentation i understand that i can attach labels when in the Meta form class but i don't fully understand how i can display dynamically all the category names instead of Categories object 1,2,3,4.
Models.py
class categories(model.Model):
id = models.AutoField(primary_key =True)
name = models.Charfield(max_length = 50)
class Event(models.Model):
id = models.AutoField(primary_key =True)
category = models.ForeignKey(Categories,on_delete=models.SET_NULL,null = True)
owner = models.Charfield(max_length = 50)
Forms.py
class EventForm(forms.ModelForm):
class Meta:
model = Event
fields = ['category','person','owner']
So the actual result when rendering the form is :
Category:(dropdown) - Categories object (1)
Desired result:
Category:(dropdown) - weddings
class categories(model.Model):
id = models.AutoField(primary_key =True)
name = models.Charfield(max_length = 50)
def __str__(self):
return self.name
class Event(models.Model):
id = models.AutoField(primary_key =True)
category = models.ForeignKey(Categories,on_delete=models.SET_NULL,null = True)
owner = models.Charfield(max_length = 50)
def __str__(self):
return self.owner
just add this little magic functions to your model classes.

Set a default hidden value with an id in a ManyToManyField

I've got these two guys in models.py:
class Tag(models.Model):
name = models.CharField(max_length=100)
movies = models.ManyToManyField(Movie)
class Movie(models.Model):
title = models.CharField(max_length=120)
release_year = models.IntegerField('release year', default=0000)
plot = models.CharField(max_length=400)
pub_date = models.DateTimeField('date published')
and this in views.py
class TagCreate(LoginRequiredMixin, CreateView):
model = Tag
success_url = '/movies/ratings/'
fields = ["name"]
Im trying to set the "movies" field in the Tag class with the value of the current movie I'm "taggin"
The URL is:
url(r'^(?P<movie_id>\d+)/tag/$', TagCreate.as_view(), name='tag'),
Thanxs in advanced!
Don't do this as a hidden field on the form. The data is in the URL, you can take it from there. The place to do this is in form_valid.
def form_valid(self):
result = super(TagCreate, self).form_valid()
movie = Movie.objects.get(pk=self.kwargs['movie_id'])
self.object.movies.add(movie)
return result

Searching in several tables with django-haystack

I've got the Restaurant and Comment models shown below. The Comment model has a ForeignKey to Restaurant. How can I perform a search in some of the Restaurant fields and in the comment field of the Comment model which returns a list of Restaurant instances?
Thanks
class Restaurant(models.Model):
name = models.CharField(max_length=100)
country=models.ForeignKey(Country)
city=models.ForeignKey(City)
street=models.CharField(max_length=100)
street_number=models.PositiveSmallIntegerField()
postal_code=models.PositiveIntegerField(blank=True, null=True)
slug = models.SlugField(unique=True)
class Comment(models.Model):
user = models.ForeignKey(User)
restaurant = models.ForeignKey(Restaurant)
submit_date = models.DateTimeField(blank = True, null = False)
comment = models.TextField()
I think you should read the manual: http://django-haystack.readthedocs.org/en/latest/tutorial.html
look for multivalue:
class RestaurantIndex(indexes.SearchIndex):
comments = indexes.MultiValueField()
def prepare_comments(self, obj):
return [a for a in obj.comment_set.all()]