[Fixed]-Django default data (Fixtures?) in views.py on form.save()?

1๐Ÿ‘

If your concern is only creating an id, you can have a model with null allowed fields and auto id field. Other fields can also have default values. default field option also allows callbacks.
ex:-

created_at = models.DateTimeField(default=datetime.now)

Look at:https://docs.djangoproject.com/en/1.9/topics/db/models/#field-options

A Model with all nullable fields or default value fields can essentially be created with a single line of code in form.save(). Assuming you intend to create a single default model object only.

As for the right way, you might want to have simple flags(to record an business profile as active leaving the default one inactive etc.) in your model and right relationships(FK) so that a user cannot repeatedly create default business profiles.

๐Ÿ‘คutkarshmail2052

0๐Ÿ‘

You can use a django post_save signal. Its easy to use.

https://docs.djangoproject.com/en/1.9/ref/signals/#django.db.models.signals.post_save

You can put the code in models.py of that application itself.

@receiver(post_save,sender=ModelName)
def do_some_operation(sender, **kwargs):
    if kwargs['created']:                 # Executes only if created 
        obj = kwargs['instance']
        # do modifications with your obj
        obj.save()
๐Ÿ‘คt0il3ts0ap

Leave a comment