如何在django中保存表条目

时间:2022-01-22 03:27:34

如何在django中保存表条目I am working on a django admin based project now i am stuck with a big thing.i want to add a field named "item_issued" in the user_profile model. in the "item issued" field there is a table which consist of 3 column "item_name","quantity" and "price".I am unable to apply this.Can u guys please help me in this? Thanks in advance

我正在研究基于django管理员的项目,现在我遇到了一件大事。我想在user_profile模型中添加一个名为“item_issued”的字段。在“已发布项目”字段中,有一个表格,其中包含3列“item_name”,“quantity”和“price”。我无法应用此项。您可以帮助我吗?提前致谢

2 个解决方案

#1


0  

If I understand you correctly you want to add a ForeignKey to your user_profile pointing to item_issued. You can accomplish that by creating a new model ItemIssued with the fields you mentioned:

如果我理解正确,您希望将ForeignKey添加到指向item_issued的user_profile。您可以通过使用您提到的字段创建一个新的模型ItemIssued来实现这一目标:

class ItemIssued(models.Model):
    item_name = models.CharField(max_length=100)
    quantity = models.IntegerField()
    price = models.FloatField()

Now, when you're having ItemIssued model you can add a ForeignKey to user_profile (I assume the model is called UserProfile):

现在,当您拥有ItemIssued模型时,可以将一个ForeignKey添加到user_profile(我假设该模型名为UserProfile):

class UserProfile(models.Model):
     ...   # your existing fields
     item_issued = models.ForeignKey(ItemIssued)

After that, don't forget to run

在那之后,别忘了跑

 python manage.py makemigrations app
 python manage.py migrate

#2


0  

Here is a starting point:

这是一个起点:

models.py:

class ItemIssued(models.Model):
    item_name = models.CharField(max_length=50)
    quantity = models.IntegerField()
    models.DecimalField(max_digits=6, decimal_places=2) #use decimal field for price values.


class UserProfile(models.Model):
    # some other fields..
    issued_items = models.ManyToManyField("ItemIssued", related_name="+issued_items", null=True, blank=True)

And if you need to use this field outside of Django Admin, views.py:

如果你需要在Django Admin之外使用这个字段,那么views.py:

user = UserProfile.objects.get(username="ali")

new_issued_item = ItemIssued.objects.get(item_name="test_item")

user.issued_items.add(new_issued_item) #add 
user.issued_items.delete(new_issued_item) #delete

items = user.issued_items.all()  # get all issued items of user

i didn't test the code. But they should work.

我没有测试代码。但他们应该工作。

#1


0  

If I understand you correctly you want to add a ForeignKey to your user_profile pointing to item_issued. You can accomplish that by creating a new model ItemIssued with the fields you mentioned:

如果我理解正确,您希望将ForeignKey添加到指向item_issued的user_profile。您可以通过使用您提到的字段创建一个新的模型ItemIssued来实现这一目标:

class ItemIssued(models.Model):
    item_name = models.CharField(max_length=100)
    quantity = models.IntegerField()
    price = models.FloatField()

Now, when you're having ItemIssued model you can add a ForeignKey to user_profile (I assume the model is called UserProfile):

现在,当您拥有ItemIssued模型时,可以将一个ForeignKey添加到user_profile(我假设该模型名为UserProfile):

class UserProfile(models.Model):
     ...   # your existing fields
     item_issued = models.ForeignKey(ItemIssued)

After that, don't forget to run

在那之后,别忘了跑

 python manage.py makemigrations app
 python manage.py migrate

#2


0  

Here is a starting point:

这是一个起点:

models.py:

class ItemIssued(models.Model):
    item_name = models.CharField(max_length=50)
    quantity = models.IntegerField()
    models.DecimalField(max_digits=6, decimal_places=2) #use decimal field for price values.


class UserProfile(models.Model):
    # some other fields..
    issued_items = models.ManyToManyField("ItemIssued", related_name="+issued_items", null=True, blank=True)

And if you need to use this field outside of Django Admin, views.py:

如果你需要在Django Admin之外使用这个字段,那么views.py:

user = UserProfile.objects.get(username="ali")

new_issued_item = ItemIssued.objects.get(item_name="test_item")

user.issued_items.add(new_issued_item) #add 
user.issued_items.delete(new_issued_item) #delete

items = user.issued_items.all()  # get all issued items of user

i didn't test the code. But they should work.

我没有测试代码。但他们应该工作。