I Want To Get Data Dynamically From Database Basis on Parameter of parent Field Where I Used Foreign Key

Code Of Models

class Product(models.Model):
    title = models.CharField(max_length=255)
    category = models.CharField(max_length=255, blank=True)
    availability = models.CharField(max_length=50)
    warranty = models.IntegerField()
    display_description = models.TextField()
    price = models.IntegerField()
    original_price = models.IntegerField()
    product_description = models.TextField()
    first_image = models.ImageField(upload_to='shop/images', default="")
    is_active = models.BooleanField(default=True)
    parent = models.ForeignKey(
        "self", on_delete=models.CASCADE, related_name="parent_category", null=True, blank=True)

    def __str__(self):
        if self.parent and self.category:
            return f"{self.parent}>{self.category}"

        elif self.category:
            return self.category
        else:
            return ""```
Code Of Views

class IphoneSe(ListView):
template_name = ‘iphonese.html’

model = Product
context_object_name = 'iphone_listings'

def get_queryset(self):
    # Define the 'parent' parameter you want to filter by
    parent_param = 'Apple>IPhone'  # You can change this to any value you want

    # Debugging: Print the 'parent_param' to check if it's correctly set
    print("Parent Parameter:", parent_param)

    # Filter products based on the selected 'parent' parameter
    queryset = self.model.objects.filter(parent__title=parent_param)

    # Debugging: Print the resulting queryset to check its contents
    print("Filtered QuerySet:", queryset)

    return queryset
When I Hit The Url Then On terminal Showing This

Parent Parameter: Apple>IPhone
Filtered QuerySet: <QuerySet >
[05/Oct/2023 09:32:56] “GET /iphonese/ HTTP/1.1” 200 36416
Not Found: /iphonese/assets/img/apple-cover.jpg
[05/Oct/2023 09:32:56] “GET /iphonese/assets/img/apple-cover.jpg HTTP/1.1” 404 8039

Do you have any instance created with title Apple>IPhone in your Product model, because Filtered QuerySet is empty as it cannot find any title that have Apple>IPhone

Check This I created parameter of parent Apple>I Phone I Want To get Data on The basis Of parent paramter of Apple>I Phone
Please Tell Me Solution of This

Okay, you have created parent Apple>I Phone and you are trying to filter with Apple>IPhone can you see the difference? It’s the space between I and Phone

But I Also Try This Apple>IPhone
For This parameter No Data is Showning

Instead of this

parent_param = 'Apple>IPhone'

try this

parent_param = 'Apple>I Phone'

I Tried Both of parent_param
But Nothing is Showing

can you share the particular data of this Apple>IPhone parent instance?

No not this one, I want the instance with title which is created as Apple>I Phone that you are using in field parent for every other instances. Open the update window of Apple>I Phone instance title

I’ve copy pasted your same code and tried it myself and its working fine
In terminal print statements as you have given

Parent Parameter: Apple>IPhone
Filtered QuerySet: <QuerySet [<Product: Iphone Se>, <Product: Iphone 12>]>

Product Model in admin only used title and parent for test.

Same code as you have given in my views as well

    parent_param = 'Apple>IPhone'  # You can change this to any value you want

    # Debugging: Print the 'parent_param' to check if it's correctly set
    print("Parent Parameter:", parent_param)

    # Filter products based on the selected 'parent' parameter
    queryset = Product.objects.filter(parent__title=parent_param)

    # Debugging: Print the resulting queryset to check its contents
    print("Filtered QuerySet:", queryset)

One of the issues here is that you’re trying to filter on the title field, but nowhere in any of the screen shots are you showing the contents of that field.

Everything you’re showing relative to the “parent” is the output of the __str__ function - and that does not show anything about the title.

Check the database directly, either using your database’s client or the Django shell, and examine the title fields to see what is in that column.

(Ecom_enva) hamza@hamza-HP-EliteBook-840-G1:~/PycharmProjects/ecommercewebiste/EcomProject$ python manage.py shell
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from shope.models import Product
>>> Product.objects.all()
<QuerySet [<Product: Apple>, <Product: Apple>I Phone>, <Product: >, <Product: >, <Product: >, <Product: >, <Product: Apple>Mac>, <Product: Samsung>, <Product: Apple>IPhone>, <Product: >]>
>>> products = Product.objects.all()
>>> for p in products:
...     print(p.title)
... 
Apple iPhone 12 Mini, 64GB, Blue - Unlocked (Renewed)
Apple iPhone 11 Pro, 256GB, Midnight Green - Unlocked (Renewed)
Apple iPhone 8, 64 GB, Gold - Fully Unlocked (Renewed)
Apple iPhone 12, 64GB, Red - Unlocked (Renewed Premium)
Apple iPhone 11 Pro, 256GB, Midnight Green - Unlocked (Renewed)
Apple iPhone XR, 64GB, Black - Unlocked (Renewed)
Apple 2020 MacBook Air Laptop M1 Chip, 13" Retina Display, 8GB RAM, 256GB SSD Storage, Backlit Keyboard, FaceTime HD Camera, Touch ID. Works with iPhone/iPad; Gold
SAMSUNG Galaxy S23 Ultra Cell Phone, Factory Unlocked Android Smartphone, 256GB, 200MP Camera, Night Mode, Long Battery Life, S Pen, US Version, 2023, Lavender
asdas
dfsdf
>>> for p in products:
...     print(p.parent)
... 
None
Apple
Apple>I Phone
None
Apple>I Phone
Apple>I Phone
Apple
None
Apple
Apple
>>> for p in products:
...     print(p.parent.title)
... 
Traceback (most recent call last):
  File "<console>", line 2, in <module>
AttributeError: 'NoneType' object has no attribute 'title'
>>> for p in products:
...     if p is not None:print(p.parent.title)
... 
Traceback (most recent call last):
  File "<console>", line 2, in <module>
AttributeError: 'NoneType' object has no attribute 'title'
>>> for p in products:
...     if p is not None:
...             print(p.parent.title)
... 
Traceback (most recent call last):
  File "<console>", line 3, in <module>
AttributeError: 'NoneType' object has no attribute 'title'
>>> for p in products:
...     if p.parent is not None:
...             print(p.parent.title)
... 
Apple iPhone 12 Mini, 64GB, Blue - Unlocked (Renewed)
Apple iPhone 11 Pro, 256GB, Midnight Green - Unlocked (Renewed)
Apple iPhone 11 Pro, 256GB, Midnight Green - Unlocked (Renewed)
Apple iPhone 11 Pro, 256GB, Midnight Green - Unlocked (Renewed)
Apple iPhone 12 Mini, 64GB, Blue - Unlocked (Renewed)
Apple iPhone 12 Mini, 64GB, Blue - Unlocked (Renewed)
Apple iPhone 12 Mini, 64GB, Blue - Unlocked (Renewed)
>>> for p in products:
...     if p.parent is not None:
...             print(p.id, "=>", p.parent.title)
...     else:
...             print(p.id, "=>", p.parent)
... 
32 => None
33 => Apple iPhone 12 Mini, 64GB, Blue - Unlocked (Renewed)
34 => Apple iPhone 11 Pro, 256GB, Midnight Green - Unlocked (Renewed)
35 => None
36 => Apple iPhone 11 Pro, 256GB, Midnight Green - Unlocked (Renewed)
37 => Apple iPhone 11 Pro, 256GB, Midnight Green - Unlocked (Renewed)
38 => Apple iPhone 12 Mini, 64GB, Blue - Unlocked (Renewed)
39 => None
40 => Apple iPhone 12 Mini, 64GB, Blue - Unlocked (Renewed)
41 => Apple iPhone 12 Mini, 64GB, Blue - Unlocked (Renewed)
>>> for p in products:
...     if p.parent is not None:
...             print(p.id, "=>", p.parent.id)
...     else:
...             print(p.id, "=>", p.parent)
... 
32 => None
33 => 32
34 => 33
35 => None
36 => 33
37 => 33
38 => 32
39 => None
40 => 32
41 => 32
>>> 

    def get_queryset(self):
        # Define the 'parent' parameter you want to filter by
        parent_param = 'Apple>IPhone'  # You can change this to any value you want

        # Debugging: Print the 'parent_param' to check if it's correctly set
        print("Parent Parameter:", parent_param)

        # Filter products based on the selected 'parent' parameter
        queryset = self.model.objects.filter(parent__title=parent_param)

        # Debugging: Print the resulting queryset to check its contents
        print("Filtered QuerySet:", queryset)

        return queryset

Now Please Solve My Problem I Am Stuck In This Problem Very Badlyy…

What is the issue?

You can see from your output what you should be searching for.

What Line of COde Which Get Dynamically Data From Database On Basis Of Parent parameter Apple> I Phone

 def get_queryset(self):
        # Define the 'parent' parameter you want to filter by
        parent_param = 'Apple>I Phone'  # You can change this to any value you want

        # Debugging: Print the 'parent_param' to check if it's correctly set
        print("Parent Parameter:", parent_param)

        # Filter products based on the selected 'parent' parameter
        queryset = self.model.objects.filter(parent__title=parent_param)

        # Debugging: Print the resulting queryset to check its contents
        print("Filtered QuerySet:", queryset)

        return queryset

Update It Please

That value - Apple > IPhone is not a single field in the database - it’s not something to search for directly. It appears it may be a concatenation of two fields.

You need to identify which fields contains the data that you want to compare and create your query accordingly.