Skip to content
Advertisement

Django is_active field is not changing

I’m using django 4.0 when i change the is_active to False from django admin, it doesn’t make any changes to the object, I have override the save method in models.

models.py

class Topic(CreatedModel, DatedModel):
    name = models.CharField(
        max_length=255,
        unique=True,
        null=False,
    )
    slug = models.SlugField(
        max_length=255,
        unique=True,
        help_text="A unique field used for creating Topics in Kafka"
    )
    category = models.CharField(
        max_length=255,
        blank=True,
        choices=CATEGORY,
        help_text="Default for constant value updates"
    )
    selected_model = models.CharField(
        max_length=255,
        choices=ALERT_MODELS,
        unique=True,
        help_text="The Model for sending updates to Kafka"
    )
    is_active = models.BooleanField(
        default=True
    )

    def save(self, *args, **kwargs):
        if self.pk is None:
            print("New object topic")
            topic_name = self.slug
            invoke_kafka_topic_create.delay(topic_name)
            super().save(*args, **kwargs)
    
    def __str__(self):
        return self.slug

admin.py

class TopicManager(admin.ModelAdmin):

   def save_model(self, request, obj, form, change):
    if getattr(obj, 'created_by', None) is None:
        obj.created_by = request.user
        obj.save()
    else:
        obj.modified_by = request.user
        obj.save()

Can anyone advise ? The problem started when i added the save() in models.py

Advertisement

Answer

The .save() method should always call the super method, regardless whether pk is None or not, so:

class Topic(CreatedModel, DatedModel):
    # …
    
    def save(self, *args, **kwargs):
        if self.pk is None:
            print('New object topic')
            invoke_kafka_topic_create.delay(self.slug)
        return super().save(*args, **kwargs)  # 🖘 outside if body
Advertisement