Django saving in a model with User field
<p>I have token auth implemented in django and my models looks like-</p>

<pre><code>class Portfolio(models.Model):
  owner = models.ForeignKey(User, verbose_name='User', null=True)
  company = models.TextField(null=True)
  volume = models.IntegerField(blank=True)
  date = models.DateField(null=True)
</code></pre>

<p>And to save in this model, I have the following in views-</p>

<pre><code>arr = []
contents = request.data
user = User.objects.filter(username=request.user)
user_is = User(username=user)
  for i in range(0, len(portfolio_contents)):
    line = portfolio_contents[i].split(",")
    get_isin = Endday.objects.get(company=line[0])
    datestuff = line[2]
    datestuff = datestuff[0:10]
    arr.append(Portfolio(owner=user_is, company=line[0], volume=line[1], date=datestuff))
  Portfolio.objects.bulk_create(arr)
</code></pre>

<p>This code saves the data but when I try to see the data, I get this-</p>

<pre><code>[
{
    "company": "BAL",
    "volume": 1425,
    "date": "2014-02-19",
    "owner": null
},
{
    "company": "RLD",
    "volume": 2245,
    "date": "2014-02-19",
    "owner": null
},
</code></pre>

<p>Owner should not be <strong>null</strong> because if I try to <code>print(user.username)</code>, it prints <code>&lt;QuerySet [&lt;User: ku&gt;]&gt;</code>.</p>

<p>What seems to be the problem?</p>

<p>Your <code>user_is</code> is a <code>User</code> model instance which is not <em>saved</em> in the database:</p>

<pre><code>user_is = User(username=user) # ??? username != user
</code></pre>

<hr>

<p>Why not do:</p>

<pre><code>user_is = User.objects.get(username=request.user.username)
</code></pre>

<p>Or make <code>owner=request.user</code> in the <code>ForeignKey</code> field in your loop.</p>

python django