How can I prepopulate this field in my models? Django

I want that when I try to create a new account, the site_name field is prepopulated with the site that has it for instance, an account under reddit should have a site_name prepopulated with reddit.com already, instead of having to pick from the list of sites

here is my class view for account creating an account:

class AccountCreateView(LoginRequiredMixin, CreateView):
    model = Account
    template_name = "account_new.html"
    fields = (
        "site_name",
        "username",
        "password",
    )

the rest of my views:

class HomePageView(ListView):
    model = Site
    template_name = "home.html"

    def get_queryset(self):
        try:
            return Site.objects.filter(author=self.request.user)
        except:
            return HttpResponse("Login please!")


class SiteDetailView(LoginRequiredMixin, UserPassesTestMixin, DetailView):
    model = Site
    template_name = "site_detail.html"

    def test_func(self):
        obj = self.get_object()
        return obj.author == self.request.user


class SiteDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
    model = Site
    template_name = "site_delete.html"
    success_url = reverse_lazy("home")

    def test_func(self):
        obj = self.get_object()
        return obj.author == self.request.user


class SiteEditView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Site
    template_name = "site_edit.html"
    fields = ("siteName",)

    def test_func(self):
        obj = self.get_object()
        return obj.author == self.request.user


class AccountDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
    model = Account
    template_name = "account_delete.html"
    slug_field = "slug_field"
    slug_url_kwarg = "slug_field"
    success_url = reverse_lazy("home")


class AccountEditView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Account
    template_name = "account_edit.html"
    fields = (
        "username",
        "password",
    )
    slug_field = "slug_field"
    slug_url_kwarg = "slug_field"


class SiteCreateView(LoginRequiredMixin, CreateView):
    model = Site
    template_name = "site_new.html"
    fields = ("siteName",)

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

my models.py:

from django.db import models
from django.urls import reverse
from django.conf import settings
from django.template.defaultfilters import slugify


class Site(models.Model):
    siteName = models.CharField(max_length=200)
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )
    slug = models.SlugField(null=False, unique=True)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.siteName

    def get_absolute_url(self):
        return reverse("site_detail", kwargs={"slug": self.slug})

    def save(self, *args, **kwargs):
        if not self.slug:
            slug_str = "%s %s" % (self.siteName, self.author)
            self.slug = slugify(slug_str)
        super(Site, self).save()


class Account(models.Model):
    site_name = models.ForeignKey(Site, on_delete=models.CASCADE)
    username = models.CharField(max_length=140)
    password = models.CharField(max_length=50)
    slug_field = models.SlugField(null=False, unique=True)

    def __str__(self):
        return self.username

    def get_absolute_url(self):
        return reverse("home")

    def save(self, *args, **kwargs):
        if not self.slug_field:
            self.slug_field = slugify(self.username)
        return super().save(*args, **kwargs)

my admin.py

from django.contrib import admin
from .models import Site, Account


class SiteAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("siteName",)}


class AccountAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug_field": ("username",)}


admin.site.register(Site, SiteAdmin)
admin.site.register(Account, AccountAdmin)

a picture of creating a new account under redditcom, still having to specify the site_name:

createaccount picture

Back to Top