2

I am working on a project that has an Investment and Invoice class in the models.py. The way they work is that a user places an investment and while saving the Investment model creates an invoice. Here is the relevant code:

class Investment(models.Model):
   ...
   def save(self, *args, **kwargs):
            current_investment = Investment.objects.get(pk=self.pk)
            create_invoice = Invoice.objects.create(investment=current_investment, 
                                                    fee_type='Mngmt.', 
                                                    amount=self.amount*fee_amount)

class Invoice(models.Model):
    user = models.ForeignKey(User) 
    investment = models.ForeignKey(Investment)
    fee_type = models.CharField(max_length=24)
    amount = models.DecimalField(max_digits=12, decimal_places=2, default=0)
    date = models.DateTimeField(default=timezone.now, blank=True)

    def __str__(self):
        return self.investment.fund.name    

So basically when the Investment class is saved an Invoice object is created, however I want to get the current user and set it to the user field in the Invoice class. Any ideas on how I can get this done? Thanks.

jpic
  • 32,891
  • 5
  • 112
  • 113
ng150716
  • 2,195
  • 5
  • 40
  • 61
  • You should do this in the view, ie. http://stackoverflow.com/questions/4865084/set-form-author-field-to-logged-in-user-before-render – jpic Jan 01 '16 at 21:35
  • Thanks for the link, but I was hoping to achieve this my Model file. Since I'm hoping to refresh the data base when an investment has ended in order to renew it create another Invoice object – ng150716 Jan 01 '16 at 21:38

2 Answers2

0

It's somehow anti-pattern to access current request and current request.user in model's scope.

MVC Architectural Pattern

Basically these operation are done within view sections. So it's better to pass the current user from responsible view for saving Investment or Invoice

kia
  • 828
  • 5
  • 12
  • I was hoping to achieve this my Models since I'm hoping to refresh the database when an investment instance has ended in order to renew it create another Invoice object automatically. – ng150716 Jan 01 '16 at 21:40
  • Cool, but I recommend to move `user` field from `Invoice` to your `Investment` Mode,l as you have already a link from `Invoice` to `Investment` via `investment = models.ForeignKey(Investment)`. To access `user` from `Invoice` you can easily use `invoice.investment.user` – kia Jan 01 '16 at 21:45
  • Yea, I literally just did that a second ago before clicking on this post to see any new solutions. It works! – ng150716 Jan 01 '16 at 21:47
0

If by "current user", you mean "request.user", then you could get the request object from thread locals, but it's not considered a good practice.

Why is using thread locals in Django bad?

django-threadlocals provides a helper for that and more information about it too.

It worked for me in the past but nowadays I'd avoid it at all cost and stick to best practice and keep my technical debt as low as possible.

Community
  • 1
  • 1
jpic
  • 32,891
  • 5
  • 112
  • 113