我仍然是Python和Django的新手。我有一个自定义用户模型。我想只显示由特定用户创建的模型数据。我尝试了以下,但不起作用。请理解我还在学习。
模型
co
查看
class Company(models.Model):
user = models.ManyToManyField(settings.AUTH_USER_MODEL)
name = models.CharField(max_length=265)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('nodisoapp:home')
模板
class CreateCompany(LoginRequiredMixin, generic.CreateView):
login_url = '/scrty/login/'
form_class = forms.Companyform
template_name = 'nodiso/create_company.html'
def form_valid(self, form):
response = super(CreateCompany, self).form_valid(form)
self.object.user.add(self.request.user)
return response
def get_queryset(self):
self.user = get_object_or_404(models.Company,user)
return models.Company.filter(user=self.user)
我很感激帮助
答案 0 :(得分:0)
如果您想使用当前登录的用户,那么使用
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
Your1Service,
Your2Service,
{ provide: Your1Service, useClass: FakeYour1Service },
{ provide: Your2Service, useClass: FakeYour2Service }
]
}).compileComponents(); // compile template and css
}));