How to enter ForeginKey values in a model with CreateView - django

I am creating a wiki and need to put in values in the model called revision. This table has a foreigkey to wikipage.
My problem is that I am unable to insert values in the revision model.
I have tried using def form_valid(self, form) like you would when entering user, without any luck.
Models.py
class Wikipage(models.Model):
title = models.CharField(max_length=100)
date_created = models.DateTimeField('Created', auto_now_add=True)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = "Wikipages"
class Revision(models.Model):
wikipage = models.ForeignKey(Wikipage, null=True, on_delete=models.CASCADE, related_name='revisions')
content = models.TextField('Content')
author = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
last_edit = models.DateTimeField('Last Edited', auto_now=True)
comment = models.TextField('Comment', blank=True)
class Meta:
verbose_name = 'Revision'
verbose_name_plural = 'Revisions'
ordering = ['-last_edit']
get_latest_by = ['last_edit']
def __str__(self):
return self.content
View.py
Class WikipageCreateView(CreateView):
template_name = 'wiki/wikipageform.html'
model = Wikipage
fields = ['title']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
The template are as simple as possible with {{ form.as_p }} and all the necessary stuff.

Related

Django Model Form field data not displaying

So I am trying to figure out why my dropdown menu will not display the list of collections for the user to pick from.
Form Screenshot: [1]: https://i.stack.imgur.com/UIrq6.png
Here is the Form.py file class used for this problem:
class ProductForm(ModelForm):
class Meta:
model = listing
fields = 'all'
_---------------------------------------------
Here is the user form VIEW:
def index(request):
form = ProductForm
if request.method == 'POST':
form = ProductForm(request.POST)
if form.is_valid():
form.save()
context = {'form':form}
return render(request, 'index.html', context)
Here is also the code for the 2 models here:
class Collection(models.Model):
title = models.CharField(max_length=255)
def __str__(self) -> str:
return self.title
class Meta:
ordering = ['title']
class listing(models.Model):
image = models.ImageField(blank=True, null=True)
name = models.CharField(max_length=255)
description = models.TextField()
unit_price = models.DecimalField(max_digits=6, decimal_places=2, validators=[MinValueValidator(1)])
inventory = models.IntegerField()
last_update = models.DateTimeField(auto_now=True)
collection = models.ForeignKey(Collection, on_delete=models.PROTECT, blank=True, null=True)
vendors = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=False)
I'm somewhat new to django and just trying to understand why for the form it won't display the list of collections in the dropdown.

Autofilling Django model form field with data from associated objects

I have a model form that creates a new job entry, and on submission, I need an invisible field job_time_estimation to be set to a sum of 'service_stats_estimate_duration' values from ServiceItemStats objects associated with the JobEntry by a many-to-many relationship when submitting the form.
For example, if in my NewJobEntryForm I chose two existing ServiceItemStats objects that have service_stats_estimate_duration values 60 and 90, on submission, I want a value 150 to be saved in that JobEntry object's job_time_estimation attribute.
I tried doing this using aggregation by defining a save() method in the model but I am getting an error "name 'serviceItemStats' is not defined".
I am not sure if I am going about this the right way. Any help would be appreciated.
My code:
models.py:
class ServiceItemStats(models.Model):
service_stats_name = models.CharField(primary_key=True, max_length=20)
service_stats_estimate_duration = models.IntegerField()
# Many-to-many relationship with JobEntry.
def __str__(self):
return self.service_stats_name
class JobEntry(models.Model):
# PK: id - automatically assigned by Django.
jo
b_entry_date_time = models.DateTimeField(default=timezone.now)
jo
b_date = models.DateField(blank=True, null=True)
job_checked_in = models.BooleanField()
job_checked_out = models.BooleanField(default=False)
job_priority = models.IntegerField()
job_time_estimation = models.IntegerField(blank=True, null=True)
job_comments = models.TextField(max_length=200, blank=True, null=True)
job_parts_instock = models.BooleanField(default=False)
job_started = models.BooleanField(default=False)
job_finished = models.BooleanField(default=False)
job_expand_fault_evidence = models.ImageField(blank=True, null=True)
job_expand_comments = models.ImageField(blank=True, null=True)
job_expand_parts_required = models.CharField(max_length=200, blank=True, null=True)
vehicle = models.ForeignKey(Vehicle, on_delete=models.CASCADE) #One-to-one relationship
customer = models.ForeignKey(Customer, on_delete=models.CASCADE) #One-to-one relationship
serviceBay = models.ForeignKey(ServiceBay, on_delete=models.CASCADE, blank=True, null=True) #One-to-one relationship
serviceItemStats = models.ManyToManyField(ServiceItemStats, blank=True) #Many-to-many relationship
def __str__(self):
return self.id
def save(self, *args, **kwargs):
if not self.job_time_estimation:
self.job_time_estimation = serviceItemStats.objects.all().aggregate('service_stats_estimate_duration')
return super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse("jobs:job_detail",kwargs={'pk':self.pk})
views.py
class JobCreateView(FormView):
template_name = "jobs/jobentry_form.html"
form_class = NewJobEntryForm
success_url = reverse_lazy("jobs:job_list")
def form_valid(self, form):
form.save()
return super(job_list, self).form_valid(form)
forms.py
class NewJobEntryForm(ModelForm):
class Meta:
model = JobEntry
fields = ['vehicle', 'customer', 'job_date', 'job_checked_in', 'job_priority', 'job_comments', 'job_parts_instock', 'serviceItemStats']
widgets = {
'job_date' : forms.DateInput(format=('%m/%d/%Y'), attrs={'class':'form-control', 'placeholder':'Select a date', 'type':'date'}),
'ServiceItemStats' : forms.CheckboxSelectMultiple(),
'job_priority' : forms.RadioSelect(choices=priorityOptions),
}
You can try this.
from django.db.models import Sum
class JobCreateView(FormView):
template_name = "jobs/jobentry_form.html"
form_class = NewJobEntryForm
success_url = reverse_lazy("jobs:job_list")
def form_valid(self, form):
job=form.save()
estimation = job.serviceItemStats.all().aggregate(total=Sum('service_stats_estimate_duration'))
job.job_time_estimation = estimation['total']
job.save()
return super(job_list, self).form_valid(form)

Easiest way to add {{ variable }} from different related model in one template

I got a question. I'm wondering what's the easiest way to add different {{ variable }} from different related model in one template.
I'd like to add some information related to seller rating:
def total_seller_ratings(self):
return self.seller.rating_seller.count()
def avg_seller_ratings(self):
return self.seller.annotate(avg_ratesugar=Avg('rating_seller__ratesugar')).order_by('-avg_ratesugar')
Create a template tag from Middleware? Or is any fastes solution to add another model on my view?
I got a main view for my main page:
#Channel MAIN PAGE
#method_decorator(login_required(login_url='/cooker/login'),name="dispatch")
class ChannelMainPage(generic.DetailView, FormMixin):
model = Channel
context_object_name = 'channel'
template_name = 'channel_detail.html'
form_class = ChannelChatForm
def get_context_data(self, **kwargs):
context = super(ChannelMainPage, self).get_context_data(**kwargs)
context['form'] = self.get_form()
return context
def form_valid(self, form):
if form.is_valid():
form.instance.channel = self.object
form.instance.user = self.request.user
form.save()
return super(ChannelMainPage, self).form_valid(form)
else:
return super(ChannelMainPage, self).form_invalid(form)
def post(self,request,*args,**kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_valid(form)
def get_success_url(self):
return reverse('channel:channel_detail',kwargs={"slug":self.object.slug})
models.py
class Channel(models.Model):
consumer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="channel_consumer", blank=True, null=True)
name = models.CharField(max_length=10)
seller = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="channel_seller")
image = models.ImageField(null=True,blank=True,default='user/user-128.png', upload_to='channel/')
date_created = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField('Make it happen', default=False)
class Rating(models.Model):
channel = models.OneToOneField(Channel,on_delete=models.CASCADE,related_name="rating_channel")
consumer = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.CASCADE,related_name='rating_consumer')
seller = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.CASCADE,related_name='rating_seller')
is_rated = models.BooleanField('Already rated', default=True)
comment = models.TextField(max_length=200)
publishing_date = models.DateTimeField(auto_now_add=True)
ratesugar = models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(5)])
def __str__(self):
return self.channel.name
One place to add it is on the Model class. This is already available as a variable within the context (see generic.Detailview.get_context_data). It is named "object" and can then be accessed through the templating mechanics.
class Channel(models.Model):
consumer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="channel_consumer", blank=True, null=True)
name = models.CharField(max_length=10)
seller = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="channel_seller")
image = models.ImageField(null=True,blank=True,default='user/user-128.png', upload_to='channel/')
date_created = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField('Make it happen', default=False)
def some_method():
pass
template.html
<div>{{ object.some_method() }}</div>
Finally, on the discussion (code design) of which model to place the desired methods on...
If you want to calculate the average over ALL ratings it belongs on the User model.
If you want an average over the ratings over a given channel then it belongs on the Channel model.
Good luck.
PS. Don't forget to prefetch_related to have avoid slow queries!

How to save the attribute of my ForeignKey?

I am trying to create a view that allows users to upload their documents in my Lessons model. However when documents are uploaded, I am not able to save the instance in which the form is being submitted. When I access the admin page, the field for my ForeignKey is left empty.
This is the views.py for users to submit their documents:
class UploadLessonView(CreateView):
model = Lesson
fields = ['title', 'file']
template_name = 'store/upload_lesson.html'
success_url = '../'
def form_valid(self, form):
form.instance.author = self.request.user
return super(UploadLessonView, self).form_valid(form)
This is the models.py for my child model:
class Lesson(models.Model):
title = models.CharField(max_length=100)
file = models.FileField(upload_to="lesson/pdf")
date_posted = models.DateTimeField(default=timezone.now)
post = models.ForeignKey(Post, on_delete=models.CASCADE, null=True, blank=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('lesson_upload', kwargs={'pk': self.pk})
For my parent model:
class Post(models.Model):
title = models.CharField(max_length=100)
image = models.ImageField(default = 'default0.jpg', upload_to='course_image/')
description = models.TextField()
price = models.DecimalField(decimal_places=2, max_digits=6)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
rating = models.IntegerField(default = 0)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk' : self.pk})
Field for Post is left empty when I submit documents.
You are not setting the Post object in Lesson with the View. So you can fix it in two ways.
One way, is to send it via url, for example:
# url
url('lesson/<int:post_id>/create/', UploadLessionView.as_view())
And use the value of post_id in View:
class UploadLessonView(CreateView):
model = Lesson
fields = ['title', 'file']
template_name = 'store/upload_lesson.html'
success_url = '../'
def form_valid(self, form):
form.instance.post = get_object_or_404(Post, pk=self.kwargs.get('post_id'))
return super(UploadLessonView, self).form_valid(form)
Two, you can add post in fields:
class UploadLessonView(CreateView):
model = Lesson
fields = ['title', 'file', 'post'] # <-- Here
template_name = 'store/upload_lesson.html'

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 %}