class PurchaseOrder(models.Model):
product = models.ManyToManyField('Product')
vendor = models.ForeignKey('VendorProfile')
dollar_amount = models.FloatField(verbose_name='Price')
class Product(models.Model):
products = models.CharField(max_length=256)
def __unicode__(self):
return self.products
Run Code Online (Sandbox Code Playgroud)
我有那个代码.不幸的是,错误来自admin.py中ManyToManyField
class PurchaseOrderAdmin(admin.ModelAdmin):
fields = ['product', 'dollar_amount']
list_display = ('product', 'vendor')
Run Code Online (Sandbox Code Playgroud)
错误说:
'PurchaseOrderAdmin.list_display [0]','product'是不受支持的ManyToManyField.
然而,当我把它编译'product'出来的list_display.那么如何'product'在list_display不给出错误的情况下显示呢?
编辑:也许一个更好的问题是你如何显示ManyToManyField的list_display?
我正在为即将到来的求职面试练习算法,我无法正确实现这一点.我也试图最大限度地提高效率.这是问题所在:
最大限度地提高销售金属棒的业务利润.如果您销售长度为L的N根金属棒,则会收到N*L*metal_price.其余较小的金属杆将被抛弃.要切割金属棒,您需要为每次切割支付cost_per_cut.您可以获得的最大利润是多少?
constraints:
lengths will be 1 to 50 elements, inclusive.
each element of length will lie in range [1,10000]
1 <= metal_price, cost_per_cut <=1000
Run Code Online (Sandbox Code Playgroud)
样本输入:
cost_per_cut =1
metal_price =10
lengths = [26,103, 59]
return: 1770
Run Code Online (Sandbox Code Playgroud)
本书如何解决这个问题,最佳长度为6.我们从第一根杆上切下4根长度为6的长度,并从中拉出2根长度.接下来我们从第2个杆上切下17个长度为6的长度,然后扔掉长度为1的一块,第三个,我们剪下9个长度为6的长度,然后扔掉一块长度为5.所以总共做了30次切割.因此,30*6*10 - 30*1 - 1770
这是我到目前为止的尝试:
def maxProfit( cost_per_cut, metal_price, lengths):
profit =0
for num in lengths:
Run Code Online (Sandbox Code Playgroud)
我只是不确定如何去做这件事.我应该迭代这些数字,看看它们可被整除的最低数字并使用它吗?有任何想法吗?
我怎样才能让它在我的价格变量下我可以自动显示一个 $?因此,它将显示 $999.99,而不是 999.99。我在 Django Admin 中这样做。
这是 admin.py
from django.contrib import admin
from purchaseorders.models import PurchaseOrder
class PurchaseOrderAdmin(admin.ModelAdmin):
fields = ['product', 'price', 'purchase_date', 'confirmed']
list_display = ('product', 'price', 'purchase_date', 'confirmed', 'po_number')
admin.site.register(PurchaseOrder, PurchaseOrderAdmin)
Run Code Online (Sandbox Code Playgroud)
这是来自models.py的一点
from django.db import models
import random
class PurchaseOrder(models.Model):
price = models.FloatField()
Run Code Online (Sandbox Code Playgroud) 我有一个向量
vector<int> vec;
Run Code Online (Sandbox Code Playgroud)
它存储着随机数{5,7,8,9,13,13,15,17}
我有一个向量,如果前一个向量的数字是素数,则将其计算为1或0
vector< int> vec_prime_number;
Run Code Online (Sandbox Code Playgroud)
因此对于前一个它将是{1、1、0、0、1、0、1}
我正在尝试使用count函数在其中仅保存质数。我在执行此操作时遇到了一些问题。
理想情况下,我希望使vec拥有{5,7,13,13,17} //换句话说
我尝试过类似的东西
int cnt = count(vec.begin(), vec.end(), vec_prime_number())
Run Code Online (Sandbox Code Playgroud)
但我什么也不能用。关于如何获取计数以仅存储质数的任何想法?
我有一个包含字符串的文件
你好,我的名字是乔
你好吗?
你好吗?
我正在尝试按原样输出该文件,但我的程序输出为"HellomynameisJoeHowAreyouDoing?Goodyou?" 我遇到空间和新线路问题.
int main (int argc, char* argv[])
{
index_table table1;
string word;
ifstream fileo;
fileo.open(argv[1]); //where this is the name of the file that is opened
vector<string> line;
while (fileo >> word){
line.push_back(word);
}
cout << word_table << endl;
for (int i=0; i < line.size(); i++)
{
if (find(line.begin(), line.end(), "\n") !=line.end())
cout << "ERRROR\n"; //My attempt at getting rid of new lines. Not working though.
cout << line[i];
}
fileo.close();
Run Code Online (Sandbox Code Playgroud)
返回0;
我的这个功能出错了.说"警告:引用局部变量'最终'返回"任何想法有什么问题吗?以及如何解决它?
vector<int> & find1(string &search_word)
{
vector<int> final;
final.push_back(2);
final.push_back(5);
return (final); //This is giving me the error when I try to return the vector.
}
int main ()
{
string search;
cin >> search;
vector <int> &p = find1(search);
}
Run Code Online (Sandbox Code Playgroud) 我有代码
vector <int> v;
v.erase(remove_if(v.begin(),v.end(),bool_checker),v.end());
Run Code Online (Sandbox Code Playgroud)
其中v是保持随机数的向量{2, 4 ,5, 6, 7, 11}和bool_checker是一个类对象,其值的数量是否是素数或不
我想删除不是素数的数字.换句话说,我想v看起来像{5, 7, 11}.现在v.erase()正在删除不是素数.所以输出是{2, 4, 6}.
我无法让编译器接受不是bool_checker.我试过了(!bool_checker),bool_checker==false等等,这些都没有奏效.有任何想法吗?
我正在读取一个文件,我正在尝试将它放到哪里如果我在地图中找到一个对象,它会将新对象的值添加到已经在地图中找到的对象中.我不知道的是如何使用正确的地图语法来做到这一点.这就是我所拥有的:
struct ap_pair {
ap_pair(float tp, float tm) : total_price(tp), total_amount(tm) {};
ap_pair & operator+=(const ap_pair &);
float total_price;
float total_amount;
};
void APC :: compute_total ()
{
string name;
map<string, ap_pair> :: iterator my_it;
float num1, num2, num3;
while (!fs.eof() )
{
fs >> name >> num1 >> num2; //read in file
ap_pair myobj(num1, num2); //send the weight/count and per unit price ap_pair
my_it = mymap.find(name); //returns iterator
if (my_it != mymap.end())
{
// myobj+= //ERROR here. how can …Run Code Online (Sandbox Code Playgroud) 我正在编制一个程序,其中列出了34个城市,我希望给这些城市中的每个城市一个被选中的概率.
所以我有:
vector<float> vec;
int s;
cin >> s;
srand(s);
for (int k=0; k < 34; k++)
{
float p1= (float)rand()/(float)((unsigned)RAND_MAX+1);
vec.push_back(p1);
}
Run Code Online (Sandbox Code Playgroud)
这样就给了每个城市一个概率.我现在遇到的问题是我想做一个随机数生成器,它将选择其中两个城市.因此,例如city1将有5%,city2为2%,city3,3%等.我如何根据给定的概率随机选择其中两个城市?