0
class Entry(models.Model):
    name = models.CharField(max_length=200, null=True, blank=True)
    city = models.CharField(max_length=200, null=True, blank=True)
    zip_code = models.ManyToManyField('EntryTwo', related_name="entries", blank=True)
    slug = models.SlugField(null=True, unique=True, max_length=300)
    id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
    
    def __str__(self):
        return self.name

    def get_absolute_url(self):
            return reverse("index", kwargs={"slug": self.slug})
    
    def save(self, *args, **kwargs):
    self.slug = slugify(f"{self.name}-{self.city}-{self.zip_code}")
    return super().save(*args, **kwargs)

Url is being returned as www.example.com/name-city-zip_code.

If an object has is missing a city, then url is returning: www.example.com/name-none-zip_code

How can I add an if statement in the F string in order to only display an object field if it is not None?

I tried the following but did not work:

self.slug = slugify(f"{self.name}-{self.city}-{self.zip_code}").replace("none","").

self.slug = slugify(f"{self.name}-{self.city}{% if self.zip_code %}-{self.zip_code}{% endif %}").

self.slug = slugify(f"{self.name}-{self.city}if self.zip_code:-{self.zip_code}").

self.slug = slugify(f"{self.name}-{self.city}if self.zip_code-{self.zip_code}").
PolarBear
  • 97
  • 5
  • Could you please show your model's field ,I want to know which fields can be blank ? – Thierno Amadou Sow Jan 01 '23 at 07:43
  • Where is the slug field in your model ? – Thierno Amadou Sow Jan 01 '23 at 08:01
  • added full info, basically, some of my objects can have blank fields, but I want the slug to ignore blank fields rather than returning "none" for the field in the slug – PolarBear Jan 01 '23 at 08:16
  • look at my answer but you will notice that i did not add the zip_code because i do not know how you EntryTwo model looks like.if you want me to add it update your question with the Entrytwo model. – Thierno Amadou Sow Jan 01 '23 at 08:52
  • 1
    Thank you, it worked. I also just found another solution which is to add a condition into the F string, for example: self.slug = slugify(f"{self.name}-{self.city}-{self.zip_code if self.zip_code is not None else ''}") – PolarBear Jan 01 '23 at 09:03
  • yeah that's a good idea too and could you show me you EntryTwo model i can add the zip_code to the url ? – Thierno Amadou Sow Jan 01 '23 at 09:05

2 Answers2

1

you can do something like this and to make sure that the slug is unique i will add the id to the slug.

class Entry(models.Model):

    ...................

    def save(self, *args, **kwargs):
        url_slug = ''
        name = self.name
        if name:
            url_slug += name +' '

        city = self.city
        if city:
            url_slug += city +' '

        id = self.id
        if id:
            url_slug += str(id)

        self.slug = slugify(url_slug.strip())
        return super().save(*args, **kwargs)
Thierno Amadou Sow
  • 2,523
  • 2
  • 5
  • 19
0

here you go:

class ZIP(models.Model):
    location = models.CharField(max_length=50, blank=True)
    created = models.DateTimeField(auto_now_add=True)
    id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

    def __str__(self):
        return self.location

Thanks again

PolarBear
  • 97
  • 5