How to surface two Django models on one page? - django

I would like to create one page that surfaces two separate Django models:
class Client(models.Model):
name = models.CharField(max_length=100)
slug = AutoSlugField(populate_from='name', blank=True, unique=True)
order = models.IntegerField(editable=False, default=0)
class Meta:
ordering = ('order',)
def __unicode__(self):
return self.name
class Press(models.Model):
title = models.CharField(max_length=50)
article = models.ImageField(upload_to = 'images')
def image_thumb(self):
if self.article:
return u'<img src="%s" height="125"/>' %self.article.url
else:
return "no image"
image_thumb.short_description = "article"
image_thumb.allow_tags = True
class Meta:
verbose_name_plural = "press"
I am unsure how to write my queryset in Views.py. I've tried something like this...
class ClientView(generic.ListView):
template_name = 'clients.html'
context_object_name = 'client'
def queryset(request):
client_page = {'press': Press.objects.all(), 'client': Clients.objects.all()}
return client_page
and then this in my urls.py...
url(r'^clients/', views.ClientView.as_view(), name = 'client_model'),
I read in a stack answer that I can do this by using "get_extra_context" but can someone show me how that's used?

class ClientView(generic.ListView):
# ...
def get_context_data(self, **kwargs):
context = super(ClientView, self).get_context_data(**kwargs)
context['press'] = Press.objects.all()
return context

Related

showing entries based on selection type in django

my question is i want to show only particular titles under music_track (musicmodel)field when type = track(title model) in my django admin site
class album(models.Model):
def get_autogenerated_code():
last_id = album.objects.values('id').order_by('id').last()
if not last_id:
return "AL-"+str(0)
return "AL-"+str(last_id['id'])
album_name = models.CharField( max_length=150, blank=False )
music_track = models.ManyToManyField("title")
def __str__(self):
return (self.album_name)
class Meta:
verbose_name = "Album"
verbose_name_plural = "Albums"
class title(models.Model):
def get_autogenerated_code():
last_id = title.objects.values('id').order_by('id').last()
if not last_id:
return "TT-"+str(0)
return "TT-"+str(last_id['id'])
upc_code = models.CharField(max_length=15, default="N/A", blank=False)
display_name = models.CharField(max_length=150, blank=False)
type = models.ForeignKey(Asset_Type, on_delete=models.CASCADE, null=True)
def __str__(self):
return (self.display_name+ " " +self.code)
admin.site.register( [album, title] )
From your question, I am understanding that while creating an album in your admin panel, you require that the music_track should only show the titles having type as track. My solution for this is:
In your admin.py file
from .models import title, album, Asset_type
class AlbumForm(forms.ModelForm):
class Meta:
model = Product
fields = ('album_name', 'music_track', )
def __init__(self, user, *args, **kwargs):
super(AlbumForm, self).__init__(*args, **kwargs)
type = Asset_type.objects.get(type='track')
self.fields['music_track'].queryset = Title.objects.filter(type=type)
class MyModelAdmin(admin.ModelAdmin):
form = AlbumForm
admin.site.register(album, MyModelAdmin)
Maybe this can give you the idea you need.

Django problem with having slug in my url while using a class

First i was using a detailslist with a def which was working fine, but i wanted to add a widget to show similar post (post in same category) so i had to change my def to a class to put a def get_context_data(self, pk), but now it always say et_context_data() got an unexpected keyword argument 'object'
here is my url
url(r'^(?P<slug>[\w-]+)$', views.postdetails.as_view(), name="postdetails"),
my view
class postdetails(DetailView):
model = Post
template_name = 'blog/post.html'
def get_context_data(self,**kwargs):
context = super(postdetail, self).get_context_data(**kwargs)
cat_id = self.kwargs.get('pk', None)
category = get_object_or_404(Category, id=cat_id)
getcat = category.post_set.all().order_by("-date")
resultcat = random.sample(getcat,4)
context['similarpost'] = resultcat
return context
and my model if needed
class Post(models.Model):
title = models.CharField(max_length = 140, unique=True)
slug = models.SlugField(max_length=40, blank=True, unique=True)
image = models.ImageField(upload_to="media", blank=True)
body = RichTextField(config_name='default')
date = models.DateField()
category = models.ManyToManyField(Category)
def __str__(self):
return self.title
I changed the pk in def get_context_data(self,pk): to **kwargs
but i get name 'postdetail' is not defined
thanks a lot

Django Form Validators on Comparing One Object Against Another

I am creating a Non Disclosure Agreement form that a user fills out after registering and logging in. I am using a custom signup form with AllAuth and pre-populating parts of the form. I pre-populate the first and last name into the top part of the form as shown below in first screen shot, but as a part of the digital signature I am setting up; I need to validate the typed signature field matches the name of the first_name and the last_name concatenated together per the second screen-shot. I know I need to setup a validator based on Django Form & Field Validations and I've tried several things but just can't get my mind rapped around it. Any help putting this together would be huge...thank you.
My Models
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="profile", verbose_name="user")
...
class NonDisclosure(Timestamp):
profile = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name="nda", verbose_name="profile")
user_signature = models.CharField(max_length=250, verbose_name='Signature')
user_street = models.CharField(max_length=250, verbose_name='Street Address')
user_city = models.CharField(max_length=250, verbose_name='City')
user_state = models.CharField(max_length=2, verbose_name='State Initials')
user_zip = models.IntegerField(verbose_name='Zip Code')
phone = models.CharField(max_length=25, verbose_name='Phone Number')
cash_on_hand = models.CharField(max_length=250, verbose_name='Cash on Hand')
value_of_securities = models.CharField(max_length=250, verbose_name='Value of Securities')
equity_in_real_estate = models.CharField(max_length=250, verbose_name='Equity on Real Estate')
other = models.CharField(max_length=250, verbose_name='Other Assets')
#property
def username(self):
return self.profile.username
#property
def first_name(self):
return self.profile.first_name
#property
def last_name(self):
return self.profile.last_name
#property
def email(self):
return self.profile.email
class Meta:
verbose_name = 'Non Disclosure Agreement'
verbose_name_plural = 'Non Disclosure Agreements'
def __str__(self):
return "%s" % self.profile
def get_absolute_url(self):
return reverse('nda_detail', args=[str(self.id)])
My Views:
class NonDisclosureForm(BaseModelForm):
cash_on_hand = forms.CharField(required=False)
value_of_securities = forms.CharField(required=False)
equity_in_real_estate = forms.CharField(required=False)
other = forms.CharField(required=False)
class Meta:
model = NonDisclosure
fields = ['user_signature', 'user_street', 'user_city', 'user_state', 'user_zip', 'phone', 'cash_on_hand', 'value_of_securities', 'equity_in_real_estate', 'other']
class NdaCreate(CreateView):
form_class = NonDisclosureForm
template_name = 'nda/nda_form.html'
def form_valid(self, form):
form.instance.profile = Profile.objects.get(user=self.request.user)
form.instance.created_by = self.request.user
return super(NdaCreate, self).form_valid(form)
Firstly, you should subclass ModelForm, not BaseModelForm. Write a clean_<fieldname> method for your user_signature field, and make sure that the value is as expected. You can access self.instance.created_by to check.
class NonDisclosureForm(ModelForm):
...
class Meta:
model = NonDisclosure
fields = ['user_signature', ...]
def clean_user_signature(self):
user_signature = self.cleaned_data['user_signature']
expected_name = '%s %s' % (self.instance.created_by.first_name, self.instance.created_by.last_name)
if user_signature != expected_name:
raise forms.ValidationError('Signature does not match')
return user_signature
Then you need to update your view so that it sets instance.created_by. You can do this by overriding get_form_kwargs.
class NdaCreate(CreateView):
def get_form_kwargs(self):
kwargs = super(NdaCreate, self).get_form_kwargs()
kwargs['instance'] = NonDisclosure(created_by=self.request.user)
return kwargs

Getting context of parent in Foreign Key - Django

This is supposed to be really simple and it is in the documents. I am trying to get the context of a ListView. For example:
"We can also add the publisher into the context at the same time, so we can use it in the template http://docs.djangoproject.com/en/1.3/topics/class-based-views/#dynamic-filtering/:
class IssuesByTitleView(ListView):
context_object_name = "issue_list"
def get_queryset(self):
self.title = get_object_or_404(Title, slug=self.kwargs['title_slug'])
return Issue.objects.filter(title=self.title).order_by('-number')
My models.py look something like this:
class Title(models.Model):
CATEGORY_CHOICES = (
('Ongoing', 'Ongoing'),
('Ongoing - Canceled', 'Ongoing - Canceled'),
('Limited Series', 'Limited Series'),
('One-shot', 'One-shot'),
('Other', 'Other'),
)
title = models.CharField(max_length=64)
vol = models.IntegerField(blank=True, null=True, max_length=3)
year = models.CharField(blank=True, null=True, max_length=20, help_text="Ex) 1980 - present, 1980 - 1989.")
category = models.CharField(max_length=30, choices=CATEGORY_CHOICES)
is_current = models.BooleanField(help_text="Check if the title is being published where Emma makes regular appearances.")
slug = models.SlugField()
class Meta:
ordering = ['title']
def get_absolute_url(self):
return "/titles/%s" % self.slug
def __unicode__(self):
return self.title
class Issue(models.Model):
CATEGORY_CHOICES = (
('Major', 'Major'),
('Minor', 'Minor'),
('Cameo', 'Cameo'),
('Other', 'Other'),
)
title = models.ForeignKey(Title)
number = models.IntegerField(help_text="Do not include the '#'.")
.........
Views.py:
class IssuesByTitleView(ListView):
context_object_name = "issue_list"
def get_queryset(self):
self.title = get_object_or_404(Title, slug=self.kwargs['title_slug'])
return Issue.objects.filter(title=self.title).order_by('-number')
def get_context_data(self):
context = super(IssuesByTitleView, self).get_context_data()
context['title'] = self.title
return context
In my list of issues within the Title, I need to return the Title and all the other properties of the Title. That up there does not work and it returns an error:
get_context_data() got an unexpected keyword argument 'object_list'
You should use **kwargs in get_context_data
def get_context_data(self, **kwargs):
context = super(IssuesByTitleView, self).get_context_data(**kwargs)
context['title'] = self.title
return context

django-piston: how to get values of a many to many field?

I have a model with ManyToManyField to another model. I would like to get all the info on a particular record (including the related info from other models) return by JSON.
How to get django-piston to display those values? I would be happy with just primary keys.
Or can you suggest another option ?
I may be wrong, but this should do it:
class PersonHandler(BaseHandler):
model = Person
fields = ('id', ('friends', ('id', 'name')), 'name')
def read(self, request):
return Person.objects.filter(...)
You need to define a classmethod on the handler that returns the many-to-many data, I don't believe Piston does this automatically.
class MyHandler(BaseHandler):
model = MyModel
fields = ('myfield', 'mymanytomanyfield')
#classmethod
def mymanytomanyfield(cls, myinstance):
return myinstance.mymanytomanyfield.all()
My code:
Models:
class Tag(models.Model):
"""docstring for Tags"""
tag_name = models.CharField(max_length=20, blank=True)
create_time = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.tag_name
class Author(models.Model):
"""docstring for Author"""
name = models.CharField(max_length=30)
email = models.EmailField(blank=True)
website = models.URLField(blank=True)
def __unicode__(self):
return u'%s' % (self.name)
class Blog(models.Model):
"""docstring for Blogs"""
caption = models.CharField(max_length=50)
author = models.ForeignKey(Author)
tags = models.ManyToManyField(Tag, blank=True)
content = models.TextField()
publish_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
def __unicode__(self):
return u'%s %s %s' % (self.caption, self.author, self.publish_time)
Handle:
class BlogAndTagsHandler(BaseHandler):
allowed_methods = ('GET',)
model = Blog
fields = ('id' 'caption', 'author',('tags',('id', 'tag_name')), 'content', 'publish_time', 'update_time')
def read(self, request, _id=None):
"""
Returns a single post if `blogpost_id` is given,
otherwise a subset.
"""
base = Blog.objects
if _id:
return base.get(id=_id)
else:
return base.all() # Or base.filter(...)
Works petty good.