Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango populate db with some data when new user is created
    text
    copied!<p>How can I populate the following tables with some default / initial data, every-time new user is created ? I know about this <a href="https://docs.djangoproject.com/en/dev/howto/initial-data/" rel="nofollow">https://docs.djangoproject.com/en/dev/howto/initial-data/</a>, but this works only when I create models. Here I want to insert some default entries when new user is created. </p> <p>Additionally, when I create a new user how can I add him to a given group with a static group id automatically ?</p> <p>models.py</p> <pre><code>from django.db import models from django.contrib.auth.models import User class Category(models.Model): name = models.CharField(max_length=50) user = models.ForeignKey(User) class Feed(models.Model): url = models.URLField() name = models.CharField(max_length=50) created = models.DateTimeField(auto_now_add=True) description = models.TextField(blank=True) category = models.ForeignKey(Category) user = models.ForeignKey(User) </code></pre> <p>views.py</p> <pre><code>def signup(request): if request.method == 'POST': form = UserCreationForm1(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect("/accounts/login") else: form = UserCreationForm1() return render(request, "registration/signup.html", { 'form': form, }) </code></pre> <p>forms.py</p> <pre><code>class UserCreationForm1(UserCreationForm): email = forms.EmailField(required=True) class Meta: model = User fields = ('username', 'email') </code></pre> <p>Many thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload