noranisa commited on
Commit
5abdff6
·
verified ·
1 Parent(s): 3b307a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -9
app.py CHANGED
@@ -29,11 +29,13 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
29
  db = SQLAlchemy(app)
30
 
31
 
32
- # --- MODEL DATABASE (STRUKTUR TABEL) ---
33
  class Category(db.Model):
34
  id = db.Column(db.Integer, primary_key=True)
35
  name = db.Column(db.String(100), unique=True, nullable=False)
36
- products = db.relationship('Product', backref='category', lazy=True)
 
 
37
 
38
  def __repr__(self):
39
  return self.name
@@ -44,7 +46,11 @@ class Product(db.Model):
44
  description = db.Column(db.Text, nullable=False)
45
  price = db.Column(db.String(50), nullable=False)
46
  image = db.Column(db.String(100), nullable=True, default=None)
 
47
  category_id = db.Column(db.Integer, db.ForeignKey('category.id'), nullable=False)
 
 
 
48
 
49
  def __repr__(self):
50
  return f'<Product {self.name}>'
@@ -113,7 +119,7 @@ def authenticate():
113
  return Response('Login Required', 401, {'WWW-Authenticate': 'Basic realm="Login Required"'})
114
 
115
 
116
- # --- PENGATURAN ADMIN PANEL (DENGAN KUSTOMISASI PRODUK) ---
117
  class AuthMixin:
118
  def is_accessible(self):
119
  auth = request.authorization
@@ -123,14 +129,8 @@ class AuthMixin:
123
 
124
  # Kelas kustom untuk mengatur tampilan Produk di panel admin
125
  class ProductAdminView(AuthMixin, ModelView):
126
- # Kolom yang akan ditampilkan di halaman DAFTAR PRODUK
127
  column_list = ('name', 'category', 'price')
128
-
129
- # Kolom yang bisa difilter
130
  column_filters = ('category',)
131
-
132
- # Kolom yang akan muncul di form CREATE dan EDIT
133
- # Menambahkan 'category' di sini akan secara otomatis membuatnya menjadi dropdown
134
  form_columns = ('category', 'name', 'description', 'price', 'image')
135
 
136
  # Kelas ini digunakan untuk model lain yang tidak perlu kustomisasi
 
29
  db = SQLAlchemy(app)
30
 
31
 
32
+ # --- MODEL DATABASE (STRUKTUR TABEL) - DENGAN RELASI YANG DIPERBAIKI ---
33
  class Category(db.Model):
34
  id = db.Column(db.Integer, primary_key=True)
35
  name = db.Column(db.String(100), unique=True, nullable=False)
36
+
37
+ # Definisikan relasi ke Product, terhubung dengan atribut 'category' di model Product
38
+ products = db.relationship('Product', back_populates='category', lazy=True)
39
 
40
  def __repr__(self):
41
  return self.name
 
46
  description = db.Column(db.Text, nullable=False)
47
  price = db.Column(db.String(50), nullable=False)
48
  image = db.Column(db.String(100), nullable=True, default=None)
49
+
50
  category_id = db.Column(db.Integer, db.ForeignKey('category.id'), nullable=False)
51
+
52
+ # Definisikan relasi kembali ke Category, terhubung dengan atribut 'products' di model Category
53
+ category = db.relationship('Category', back_populates='products')
54
 
55
  def __repr__(self):
56
  return f'<Product {self.name}>'
 
119
  return Response('Login Required', 401, {'WWW-Authenticate': 'Basic realm="Login Required"'})
120
 
121
 
122
+ # --- PENGATURAN ADMIN PANEL ---
123
  class AuthMixin:
124
  def is_accessible(self):
125
  auth = request.authorization
 
129
 
130
  # Kelas kustom untuk mengatur tampilan Produk di panel admin
131
  class ProductAdminView(AuthMixin, ModelView):
 
132
  column_list = ('name', 'category', 'price')
 
 
133
  column_filters = ('category',)
 
 
 
134
  form_columns = ('category', 'name', 'description', 'price', 'image')
135
 
136
  # Kelas ini digunakan untuk model lain yang tidak perlu kustomisasi