Как я могу предварительно заполнить это поле в моих моделях? Django
Я хочу, чтобы при попытке создать новый аккаунт, в поле site_name предварительно заполнялось имя сайта, на котором он есть например, для аккаунта на reddit имя_сайта должно быть уже заполнено reddit.com, вместо того, чтобы выбирать из списка сайтов
вот представление моего класса для создания учетной записи:
class AccountCreateView(LoginRequiredMixin, CreateView):
model = Account
template_name = "account_new.html"
fields = (
"site_name",
"username",
"password",
)
остальные мои взгляды:
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)
картинка создания нового аккаунта под redditcom, при этом все равно приходится указывать имя_сайта: