I have Django Model that has a live boolean attribute.
I want the View to get the Model by slug and only go to this page if it's live USING THE DetailView (not function based view, because I want to see how to do it.
Model definition
# myapp/models.py
class MyModel(models.Model):
name = models.CharField(max_length=255)
live = models.BooleanField(default=True)
slug = models.SlugField()
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
I hoped it would be done something like this:
class ModelDetailView(DetailView):
model = MyModel
def get(self, request, *args, **kwargs):
service = self.get_object_or_404(Service, live=True) # <- Main point of what I'm looking for help with
return super().get(request, *args, *kwargs)
Is there a way to filter this way?
You can specify the queryset to filter, so:
class ModelDetailView(DetailView):
model = MyModel
queryset = MyModel.objects.filter(live=True)
You thus do not need to implement the .get(…) method at all.
Related
Hello Awesome People
I wonder if there is a way to generate slug, rather than overriding the save() methods of my models.
Here's how I used to do:
def save(self, *args, **kwargs):
if self.pk is None:
self.slug = create_slug(self)
super(ModelName, self).save(*args, **kwargs)
create_slug is a function that generates the slug with slugify
def create_slug(instance,new_slug=None,field="name"):
''' long stuff to ensure the slug is unique '''
return slug
As django can do in Django Admin by automatically filling the slug field. Possible that we can simply add
an additional argument models.SlugField(field='name'). by adding this argument, django will make sure to take the value of field = name before saving to generate the slug without always overriding the save() method by copy/paste a lot of code to generate a slug for each model that We have.
I'm a little tired of copying this for all models, any hint will help!
Thank you in advance!
No, there is no such argument on model level. You can do it on admin level using prepopulated_fields:
class ArticleAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}
On the model level you can write custom mixin class and use it as parent for all models:
class SlugMixin:
def create_slug(instance,new_slug=None,field="name"):
''' long stuff to ensure the slug is unique '''
return slug
def save(self, *args, **kwargs):
if self.pk is None:
self.slug = create_slug(self)
super().save(*args, **kwargs)
class SomeModel(SlugMixin, models.Model):
...
class OtherModel(SlugMixin, models.Model):
...
In this case you don't need to implement create_slug and override save in each models.
class Report(models.Model):
# ....
product = models.ForeignKey(Product)
class Product(models.Model):
name = models.CharField(max_length=50)
class Item(models.Model):
box = models.ForeignKey(BoxInTransport)
product = models.ForeignKey(Product)
class BoxInTransport(models.Model):
transport = models.ForeignKey(Transport)
box = models.ForeignKey(Box)
This is - in short - the structure of models.
And I have a view which lets me create new report:
class ReportCreateView(CreateView):
model = Report
form_class = ReportForm
def get_form_kwargs(self):
# updating to get argument from url
kwargs = super(DifferenceCreateView, self).get_form_kwargs()
kwargs.update(self.kwargs)
return kwargs
and the form:
class ReportForm(ModelForm):
class Meta:
model = Report
fields = [
'product'
]
def __init__(self, box_nr=None, *args, **kwargs):
super(ReportForm, self).__init__(*args, **kwargs)
self.fields['product'].queryset = ???
How can I get only these products which belong to a specific box? To be more clear:
Only products which:
Item.objects.filter(box__box__box_code=box_nr)
Now I get all Items which I need, but I need to pass self.fields['products'] to only product form with this new Items queryset.
Can you help me?
EDIT
I've tried something like this:
def __init__(self, box_nr=None, *args, **kwargs):
super(ReportForm, self).__init__(*args, **kwargs)
queryset = Item.objects.filter(
box__box__box_code=boxno
)
none_queryset = Product.objects.none()
list_or_products = [p.product for p in queryset]
product_queryset = list(chain(none_queryset, list_or_products))
self.fields['product'].queryset = product_queryset
But, first - it looks little ugly :), second - it doesn't work:
AttributeError: 'list' object has no attribute 'all'
Your __init__ could look something like this:
def __init__(self, box_nr=None, *args, **kwargs):
super(ReportForm, self).__init__(*args, **kwargs)
qs = Product.objects.filter(item__box__box__box_code=box_nr)
self.fields['product'].queryset = qs
Basically, you need a reverse lookup on Product to Item. You can read the relevant documentation here
Note that: item__box__box__box_code=box_nr is based on my understanding of your models. item__box does the reverse lookup. Rest might need some tweaking based on your model definitions.
I have two Models named Sponsor and Event and they are something like that:
class Sponsor(models.Model):
name = models.CharField(max_length=200)
user = models.ForeignKey(User)
class Event(models.Model):
name = models.CharField(max_length=200)
sponsor = models.ForeignKey(Sponsor)
Then, I'm using class based views to Update my Object, and is something like that:
class EventUpdate(UpdateView):
model = Event
form_class = EventForm
success_url = reverse_lazy('dashboard_events')
def get_form_kwargs(self):
kwargs = super(EventUpdate, self).get_form_kwargs()
kwargs.update({'user': self.request.user})
return kwargs
Everything is fine so far! If I don't change my EventForm, my UpdateView works pretty well. The only problem is that I have to change my Form and filter my "sponsor" queryset before render it! That's because the user only can see the "sponsors" that he has created.
So, that's what I'm trying to achieve:
class EventForm(ModelForm):
...
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(EventForm, self).__init__(*args, **kwargs)
choices = Sponsor.objects.filter(user=self.user).values_list('pk', 'name')
self.fields['sponsor'] = forms.ChoiceField(choices=choices)
My view is redering right, only sponsors that the user has created, BUT, when I try to save, I get this error:
"Cannot assign "u'2'": "Event.sponsor" must be a "Sponsor" instance."
What should I do? I have no clue how to solve this... Am I following the right logic here? Thanks!
You're on the right track. Try something like this:
class EventForm(ModelForm):
...
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(EventForm, self).__init__(*args, **kwargs)
self.fields['sponsor'].queryset = Sponsor.objects.filter(user=self.user)
I'm trying to override concept queryset in my child form, to get a custom list concepts based on the area got from request.POST, here is my list of concepts, which i need to filter based on the POST request, this lists is a fk of my child form (InvoiceDetail). is it possible to have these filters?
after doing some test when I pass the initial data as the documentation says initial=['concept'=queryset_as_dict], it always returns all the concepts, but i print the same in the view and its ok the filter, but is not ok when i render in template, so I was reading that I need to use some BaseInlineFormset. so when I test I obtained different errors:
django.core.exceptions.ValidationError: ['ManagementForm data is missing or has been tampered with']
'InvoiceDetailFormFormSet' object has no attribute 'fields'
so here is my code:
models.py
class ConceptDetail(CreateUpdateMixin): # here, is custom list if area='default' only returns 10 rows.
name = models.CharField(max_length=150)
area = models.ForeignKey('procedure.Area')
class Invoice(ClusterableModel, CreateUpdateMixin): # parentForm
invoice = models.SlugField(max_length=15)
class InvoiceDetail(CreateUpdateMixin): # childForm
tax = models.FloatField()
concept = models.ForeignKey(ConceptDetail, null=True, blank=True) # fk to override using custom queryset
invoice = models.ForeignKey('Invoice', null=True, blank=True)
views.py
class CreateInvoiceProcedureView(LoginRequiredMixin, PermissionRequiredMixin, CreateView):
template_name = 'invoice/invoice_form.html'
model = Invoice
permission_required = 'invoice.can_check_invoice'
def post(self, request, *args, **kwargs):
self.object = None
form = InvoiceForm(request=request)
# initial initial=[{'tax': 16, }] removed
invoice_detail_form = InvoiceDetailFormSet(request.POST, instance=Invoice,
request=request)
return self.render_to_response(
self.get_context_data(
form=form,
invoice_detail_form=invoice_detail_form
)
)
forms.py
class BaseFormSetInvoice(BaseInlineFormSet):
def __init__(self, *args, **kwargs):
# call first to retrieve kwargs values, when the class is instantiated
self.request = kwargs.pop("request")
super(BaseFormSetInvoice, self).__init__(*args, **kwargs)
self.queryset.concept = ConceptDetail.objects.filter(
Q(area__name=self.request.POST.get('area')) | Q(area__name='default')
)
class InvoiceForm(forms.ModelForm):
class Meta:
model = Invoice
fields = ('invoice',)
class InvoiceDetailForm(forms.ModelForm):
class Meta:
model = InvoiceDetail
fields = ('concept',)
InvoiceDetailFormSet = inlineformset_factory(Invoice, InvoiceDetail,
formset=BaseFormSetInvoice,
form=InvoiceDetailForm,
extra=1)
How can i fix it?, what do i need to read to solve this problem, I tried to debug the process, i didn't find answers.
i try to do this:
def FooForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(FooForm, self).__init__(*args, **kwargs)
self.fields['concept'].queryset = ConceptDetail.objects.filter(area__name='default')
In a inlineformset_factory how can do it?.
After a lot of tests, my solution is override the formset before to rendering, using get_context_data.
def get_context_data(self, **kwargs):
context = super(CreateInvoiceProcedureView, self).get_context_data(**kwargs)
for form in context['invoice_detail_form']:
form.fields['concept'].queryset = ConceptDetail.objects.filter(area__name=self.request.POST.get('area'))
return context
I have the following model that basically stores a random hash value for each tag associated with a particular user.
class PublicTags(models.Model):
tag = models.ForeignKey(Tag, related_name='hashes')
hash_value = models.CharField(max_length=103)
user = models.ForeignKey(User, related_name='tags')
public = models.BooleanField(default=False)
class Meta:
unique_together = ('tag', 'user')
verbose_name_plural = 'Public Tag Hashes'
def __unicode__(self):
return u'%s: %s' % (self.tag, self.user, self.hash_value)
I am trying to use the Django Rest Framework to do the following:
Create a view that will be accessed at api/v1/public_tags.
I will use AJAX to post data using the Django Rest API.
On a post, the system does the following:
It checks to see if there is already a Public Tag row for the tag id (sent via post)
If not, it creates a random hash_value for that tag and user.
I am confused about how to use the Django Rest Framework to accomplish this task.
I got this far:
class PublicTagView(RetrieveUpdateAPIView):
model = PublicTags
serializer_class = serializers.PublicTagSerializer
permission_classes = (IsAuthenticated,)
class RetrieveUpdateAPIView(mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
generics.SingleObjectAPIView):
"""
Concrete view for retrieving or updating a model instance.
FIXME: the newest version of rest_framework has this class
"""
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
def create(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
As far as I understand you need to query an individual record and create a record.
In this case generic class based views are fine enough to start with.
You can then map these views in your urls.py like this:
urlpatterns = patterns('',
url(r'^api/v1/public_tags/$', views.PublicTagsList.as_view()),
url(r'^api/v1/public_tags/(?P<pk>[0-9]+)/$', views.PublicTagsDetail.as_view()),
)
This is valid if you use primary key to fetch individual record.