Hi,
I have a 'company' model, and each company, when created via a form, has a given number of assets. The fun bit, is that the asset is a model in its own right. To achieve this, I've done the following:
class CompanyCreateView(LoginRequiredMixin, CreateView):
form_class = CompanyCreationForm
template_name = 'create_company.html'
redirect_unauthenticated_users = True
success_url = '/users/my_companies'
def form_valid(self, form):
form.instance.admin = self.request.user
form.instance.is_validated = False
company = form.save()
for n in range(form.instance.asset_number):
Asset.objects.create_asset(company, company.admin)
some other stuff
return super().form_valid(form)
This works fine, but sometimes, the asset number is large (100,000). This causes the submission of the form to be incredibly slow (about a minute).
Since it is not necessary for the asset models to be available immediately (there is a whole email validation process before the user can actually access anything), could I make the creation of the Asset objects async, so the form is accepted immediately, and the process to create the Assets finishes whenever it's finished?
The manager is like this:
class AssetManager(models.Manager):
def create_asset(self, company, owner):
Asset = self.create(company=company, owner=owner)
return asset
Is this possible, or am I barking up the wrong tree?
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9B3BC2D3-5E81-41C0-B517-12BBA514EC3B%40googlemail.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment