[Answer]-Populate form from database model

1๐Ÿ‘

You should take a look at the Django Model Form documentention, especially the providing initial values part.

From the docs :

class Article(models.Model):
    headline = models.CharField(max_length=200, null=True, blank=True,
                            help_text="Use puns liberally")
    content = models.TextField()


class ArticleForm(ModelForm):
    class Meta:
        model = Article

>>> article = Article.objects.get(pk1=)
>>> article.headline
'My headline'
>>> form = ArticleForm(initial={'headline': 'Initial headline'), instance=article)
>>> form['pub_date'].value()
'Initial headline'

0๐Ÿ‘

You can pass initial data for the admin form using the GET parameters. For example:

/admin/app/person/add/?name=some+name&surname=surname&address=some+street

will open Person creation form with prepopulated fields.

๐Ÿ‘คcatavaran

0๐Ÿ‘

Django got your back. Check out form.initial

form = YourForm(intial={'name': name ...} )

Then you can render the form manually in your template.

๐Ÿ‘คMartol1ni

Leave a comment