Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
django-Filter: Only show filter options contained in queryset

I'm using the excellent [django-filter](https://github.com/carltongibson/django-filter) app to filter a queryset of model objects. Let's say my model and view look as follows:

#model
[...]
class Animal(models.Model):
REGION_CHOICES = (
(0, 'Africa'),
(1, 'Europe'),
)

name = models.CharField(max_length=100)
region = models.CharField(choices=REGION_CHOICES, max_length=100)
[...]

#view
[...]
qs = Animal.objects.all()
filter = AnimalFilter(request.GET, qs)
[...]


Assuming there are two animals in the database:

zebra = Animal(name='Zebra', region='Africa')
frog = Animal(name='Frog', region'Europe')

When I render the filter in the template, I correctly get a selector for region which contains the two options Europe and Africa.

But if I use some logic in the view to .exclude() objects from the queryset like this:

#view
[...]
qs = Animal.objects.all().exclude(name='Frog')
filter = AnimalFilter(request.GET, qs)
[...]

Now if I render the filter in the template I still get the two optione Europe and Africa for region although the queryset only contains one animal which has the region Africa.

Any ideas how I could get the correct options for the region field rendered in the template? Any help is greatly appreciated.

/r/djangolearning
https://redd.it/7d32ck
[AF] Flask/SQL join question

Given the format below, how do I set up an SQLAlchemy query whereby I can search the approver or creator "column" along with a status. Obviously there is a join between Users and ISG somewhere, but I'm at a loss as to how to go about it.


Something like: models.ISG.filter(models.ISG.coordinator_status == 'New').filter(models.ISG.creator.name)", but that, you know, works.... ;)



#FORM
class searchForm(FlaskForm):
search_by = SelectField("Search Field", choices=[("approver", "Approved By"),("creator", "Created By")])
search_term = search_term = TextField('Search text')
status = SelectField("Coordinator Status", choices=[("New", "New"),("Pending", "Pending")])


#MODEL
class Users(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128), index=True, unique=True)

class ISG(db.Model):
__tablename__ = 'isg'
id = db.Column(db.Integer, primary_key=True)
status = db.Column(db.String(30), unique=False)
approver_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
approver = db.relationship('Users', lazy='joined', foreign_keys=[approver_id])
creator_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
creator = db.relationship('Users', lazy='joined', foreign_keys=[creator_id])

/r/flask
https://redd.it/7s8ixp
What’s best way to do multi level inheritance Django classes?

I am making a site to showcase and classify meteorites. I am going to organize using NASA chart below. Meteorite is top then the undifferentiated or differentiated meteorites which take on the basic information from meteorite plus there own, etc. can I do this 4 or 5 levels deep and still inherit information from upper levels?

New to Django did a few demos and ready to do my first project with a purpose.

I was planning on using Multi-table inheritance

Was just wondering if anyone has any tips or advice or links.


https://docs.djangoproject.com/en/3.0/topics/db/models/#model-inheritance

[nasa meteorite classes](https://imgur.com/gallery/DhCfzv5)

/r/djangolearning
https://redd.it/gbyxrx
(Help Needed) Trying to make DateField Input set to date

Hi all,

I've been trying to have a datepicker for my DateField but it rendered as text instead of date. I understand from the Django documentation that by default the input is text, here is my code trying to change the input to date. (I am using crispy forms)

​

https://preview.redd.it/qhsvozgk5lta1.png?width=813&format=png&auto=webp&v=enabled&s=dc9decb66919fd7e334d416d6685d2ced3ea0a18

Inspect browser

<input type="text" name="close_date" class="dateinput form-control" id="id_close_date">

&#x200B;

#Model
class Tender(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=255)
description = models.TextField()
issue_date = models.DateField(default=timezone.now)
close_date = models.DateField(blank=True)
proposal_files = models.FileField(
verbose_name="Tender Proposal ", upload_to="tender_docs"
)
category = models.ForeignKey(
Category,
on_delete=models.SET_NULL,
blank=True,


/r/django
https://redd.it/12kczuu